Parcourir la source

Login/register works

Benoît Hubert il y a 7 ans
Parent
commit
5bf29501be

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@ geckodriver.log
 virtualenv
 *.pyc
 __pycache__
+debug.log

+ 18 - 0
accounts/templates/registration/login.html

@@ -0,0 +1,18 @@
+<html>
+  <title>MovieLib</title>
+    <body>
+      <h1>Your Movie list</h1>
+      {% if form.errors %}
+      <p>Your username and password didn't match. Please try again.</p>
+      {% endif %}
+
+      <h2>Login</h2>
+      {{ user }}
+      <form method="POST" action="{% url 'login' %}">
+        <input name="username" id="input-identifier" placeholder="Enter your username or password" required />
+        <input type="password" name="password" id="input-password" placeholder="Enter your password" required />
+        <input type="submit" value="Register">
+        {% csrf_token %}
+      </form>
+  </body>
+</html>

+ 1 - 1
accounts/templates/register.html

@@ -4,7 +4,7 @@
       <h1>Your Movie list</h1>
 
       <h2>Register</h2>
-      <form method="POST" action="/register">
+      <form method="POST" action="/accounts/register/">
         <input name="username" id="input-username" placeholder="Enter your username" required />
         <input name="email" id="input-email" placeholder="Enter your email" required />
         <input type="password" name="password" id="input-password" placeholder="Enter your password" required />

+ 1 - 1
accounts/tests.py

@@ -26,7 +26,7 @@ class RegisterPageTest(TestCase):
 
         self.assertIn('<h2>Register</h2>', html)
 
-        self.assertTemplateUsed(response, 'register.html')
+        self.assertTemplateUsed(response, 'registration/register.html')
 
     def test_can_save_a_POST_request(self):
         username = 'johndifool' + id()

+ 3 - 2
accounts/views.py

@@ -1,5 +1,6 @@
 from django.shortcuts import redirect, render
 from django.contrib.auth.models import User
+from django.contrib.auth.hashers import make_password
 
 # Create your views here.
 def register(request):
@@ -7,8 +8,8 @@ def register(request):
         User.objects.create(
             username=request.POST['username'],
             email=request.POST['email'],
-            password=request.POST['password'],
+            password=make_password(request.POST['password']),
         )
         return redirect('/libraries/' + request.POST['username'] + '/')
 
-    return render(request, 'register.html')
+    return render(request, 'registration/register.html')

+ 26 - 0
movielib/settings.py

@@ -115,6 +115,32 @@ USE_L10N = True
 
 USE_TZ = True
 
+LOGGING = {
+    'version': 1,
+    'disable_existing_loggers': False,
+    'handlers': {
+        'file': {
+            'level': 'DEBUG',
+            'class': 'logging.FileHandler',
+            'filename': '/home/bhu/Dev/python-tdd/debug.log',
+        },
+    },
+    'loggers': {
+        'django': {
+            'handlers': ['file'],
+            'level': 'DEBUG',
+            'propagate': True,
+        },
+    },
+}
+
+PASSWORD_HASHERS = [
+    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
+    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
+    'django.contrib.auth.hashers.Argon2PasswordHasher',
+    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
+    'django.contrib.auth.hashers.BCryptPasswordHasher',
+]
 
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/2.0/howto/static-files/

+ 3 - 2
movielib/urls.py

@@ -14,14 +14,15 @@ Including another URLconf
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
 from django.contrib import admin
-from django.urls import path
+from django.urls import include, path
 from django.conf.urls import url
 from movies import views as movie_views
 from accounts import views as account_views
 
 urlpatterns = [
     path('admin/', admin.site.urls),
+    path('accounts/', include('django.contrib.auth.urls')),
     url(r'^$', movie_views.home_page, name='home'),
     path('libraries/<str:username>/', movie_views.view_list),
-    url(r'^register/?$', account_views.register, name='register'),
+    url(r'^accounts/register/$', account_views.register, name='register'),
 ]