Bu geçici çözümü bulana kadar uzun süredir bunun cevabını bulmakta zorlanıyordum.
Models.py do: django.db'den içe aktarma modelleri
class YourLoginModel:
fullname = models.CharField(max_length=255, default='your_name', unique=True)
email = models.EmailField(max_length=255, unique=True)
username = models.CharField(max_length=255, unique=True)
password = models.CharField(max_length=255)
Forms.py'da şunları yapın:
from django import forms
from .models import YourLoginModel
class LoginForm(forms.ModelForm):
class Meta:
model = YourLoginModel
fields = ('username', 'password')
Views.py oturum açma mantığında:
def login(request):
form = LoginForm()
if request.method == 'POST':
try:
user_logging_in = User.objects.get(username=request.POST['username'])
hash = user_logging_in.password
password = request.POST['password']
except ObjectDoesNotExist:
html_response = 'User does not exists'
return HttpResponse(html_response)
if check_password_hash(hash, password):
request.session['username'] = request.POST['username']
request.session.set_expiry(0)
return redirect('yourapp:home')
else:
return HttpResponse('password was incorrect')
html = 'Login'
return render(request, 'login.html', {'form': form})
Uygulama görünümünde do üzerinde login_required gerçekleştirmek istiyorsunuz
from django.views.generic import TemplateView
class yourTemplateView(TemplateView):
template_name = 'your_template.html'
def dispatch(self, request, *args, **kwrags):
if not request.session.has_key('username'):
return redirect('yourapp:login.html')
else:
return render(request, 'your_view.html')