Login and registering

This commit is contained in:
teras
2017-10-07 12:32:56 +03:00
parent 22acfdd029
commit 81e3af7f7d
9 changed files with 279 additions and 25 deletions

View File

@@ -1,6 +1,20 @@
from django.db import models
'''class User:
user
class Challenge:
class ChallengeTag:
id =
name
description
class UserChallenge:
id
user
challenge
'''
class Challenge(models.Model):
#id =
name = models.CharField(256)
description = models.TextField()
#tags

View File

@@ -22,5 +22,6 @@ from challenges import views
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
url(r'^login/', views.login_view),
url(r'^register/', views.register),
] + static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS)

View File

@@ -3,7 +3,7 @@ from django.template import loader, RequestContext
from django.shortcuts import render_to_response, render
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
def index(request):
if request.method == 'GET':
@@ -14,16 +14,41 @@ def index(request):
def register(request):
if request.method == 'GET':
return render(request, 'register.html')
#return HttpResponse(loader.get_template('register.html').render())
elif request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
password_confirmation = request.POST['password_confirmation']
if len(password) < 8:
return HttpResponse("password too short")
if password != password_confirmation:
return HttpResponse("passwords do not match")
user = User.objects.create_user('username', 'email@email.email', password)
user.save()
if not User.objects.filter(username=username).exists():
user = User.objects.create_user(username, password=password)
user.save()
else:
return HttpResponse("user exist")
return HttpResponse("DONE")
return HttpResponse("User {} created".format(username))
@csrf_protect
def login_view(request):
if request.method == 'GET':
auth_user = 'no user'
if request.user.is_authenticated:
auth_user = request.user.username
return render(request, 'login.html', {'auth_user': auth_user})
elif request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return HttpResponse('request suq')
else:
return HttpResponse('invalid username or password')