Commit 14 Wordbucket : add login system (django build-in)
Commits on Mar 22, 2018
settings.py
- เพิ่ม url หลัง login จะ redirect ไปที่ url นี้
| @@ -27,6 +27,8 @@ |
| | |
| | ALLOWED_HOSTS = [] |
| | |
| | +# LOGIN REDIRECT URL |
| | +LOGIN_REDIRECT_URL = '/' |
| | |
| | # Application definition |
| | |
|
urls.py
- import url สำหรับ login system build-in ของ django
| @@ -14,6 +14,7 @@ |
| | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) |
| | """ |
| | from django.contrib import admin |
| | +from django.contrib.auth import views as auth_views |
| | from django.urls import include, path, re_path |
| | from wordbucket import views |
| | from wordbucket import urls as wordbucket_urls |
|
/templates/detail.html
- เพิ่มแถบแสดงสถานะ การ Authenticate ของ user
| @@ -4,6 +4,15 @@ |
| | </head> |
| | <body> |
| | <h1>Word Bucket</h1> |
| | + {% if user.is_authenticated %} |
| | + Hi {{ user.username }}! |
| | + <p><a href="{% url 'wordbucket:logout' %}">logout</a></p> |
| | + {% else %} |
| | + You are not logged in |
| | + <a href="{% url 'wordbucket:login' %}">LOGIN</a> |
| | + <br> |
| | + {% endif %} |
| | + |
| | <h4>{{ d_message }}</h4> |
| | <form method="POST" id="form1" action="/{{ word.id }}/add_explanation"> |
| | <input name="explanation_input" id="id_new_eplanation" placeholder="Add explanation" /> |
|
/templates/home.html
- เพิ่มแถบแสดงสถานะ การ Authenticate ของ user
| @@ -4,6 +4,15 @@ |
| | </head> |
| | <body> |
| | <h1>Word Bucket</h1> |
| | + {% if user.is_authenticated %} |
| | + Hi {{ user.username }}! |
| | + <p><a href="{% url 'wordbucket:logout' %}">logout</a></p> |
| | + {% else %} |
| | + You are not logged in |
| | + <a href="{% url 'wordbucket:login' %}">LOGIN</a> or <a href="{% url 'wordbucket:signup' %}">SIGNUP</a> |
| | + <br> |
| | + {% endif %} |
| | + |
| | <br>browse<br> |
| | {% for alphabet in alphabets %} |
| | <a href="{% url 'wordbucket:search' alphabet %}">{{ alphabet }}</a> |
/templates/login.html
- หน้า template login พื้นฐานของ django โดยเพิ่มปุ่มกลับหน้า home ของ wordbucket เข้าไป
| | @@ -0,0 +1,17 @@ |
| | +<html> |
| | + <head> |
| | + <title>Word Bucket : LOGIN</title> |
| | + </head> |
| | + <body> |
| | + <h1>Word Bucket</h1> |
| | + <br><h2><a href="{% url 'wordbucket:login' %}">LOGIN</a></h2><br> |
| | + |
| | + <form method="post"> |
| | + {% csrf_token %} |
| | + {{ form.as_p }} |
| | + <button type="submit">Login</button> |
| | + </form> |
| | + <a href="{% url 'wordbucket:home' %}">HOME</a></td></tr> |
| | + |
| | + </body> |
| | +</html> |
/templates/search.html
- เนื่องจาก ไม่ได้ add template ที่อธิบายไว้ที่ (
link)
- เพิ่มแถบแสดงสถานะ การ Authenticate ของ user
| | @@ -0,0 +1,37 @@ |
| | +<html> |
| | + <head> |
| | + <title>Word Bucket</title> |
| | + </head> |
| | + <body> |
| | + |
| | + <h1>Search Word Bucket </h1> |
| | + {% if user.is_authenticated %} |
| | + Hi {{ user.username }}! |
| | + <p><a href="{% url 'wordbucket:logout' %}">logout</a></p> |
| | + {% else %} |
| | + You are not logged in |
| | + <a href="{% url 'wordbucket:login' %}">LOGIN</a> |
| | + <br><br> |
| | + {% endif %} |
| | + <form action = "/search/byword" method="POST"> |
| | + <input tpye="text" name = "search_input" placeholder="Search word" /> |
| | + <br> |
| | + <input type = "submit" value = "Submit"> |
| | + {% csrf_token %} |
| | + </form> |
| | + {% if message %} |
| | + <p>{{ message }}</p> |
| | + {% endif %} |
| | + |
| | + {% if word_found %} |
| | + <table id="id_word_table"> |
| | + {% for word in word_found %} |
| | + <tr><td id="word.word"><a href="{% url 'wordbucket:detail' word.id %}">{{ word.word }}</a></td></tr> |
| | + {% endfor %} |
| | + </table> |
| | + {% endif %} |
| | + |
| | + <br> |
| | + <a href="{% url 'wordbucket:home' %}">HOME</a></td></tr> |
| | + </body> |
| | +</html> |
/templates/signup.html
- หน้า template signup พื้นฐานของ django โดยเพิ่มปุ่มกลับหน้า home ของ wordbucket เข้าไป
| | @@ -0,0 +1,16 @@ |
| | +<html> |
| | + <head> |
| | + <title>Word Bucket : LOGIN</title> |
| | + </head> |
| | + <body> |
| | + <h1>Word Bucket</h1> |
| | + <h2>Sign up</h2> |
| | + <form method="post"> |
| | + {% csrf_token %} |
| | + {{ form.as_p }} |
| | + <button type="submit">Sign up</button> |
| | + </form> |
| | + <a href="{% url 'wordbucket:home' %}">HOME</a></td></tr> |
| | + |
| | + </body> |
| | +</html> |
urls.py
- import และเพิ่ม path ของ login build-in ของ django เข้าไป (login signup และ logout)
| | @@ -1,4 +1,5 @@ |
| | from django.urls import path, re_path |
| | +from django.contrib.auth import views as auth_views |
| | from wordbucket import views |
| | |
| | app_name = 'wordbucket' |
| @@ -10,4 +11,9 @@ |
| | re_path(r'^(\d+)/add_explanation$', views.add_explanation, name='add_explanation'), |
| | re_path(r'^(\d+)/like$', views.vote_like, name='like'), |
| | re_path(r'^(\d+)/dislike$', views.vote_dislike, name='dislike'), |
| | + |
| | + # auth part |
| | + re_path(r'^signup/$', views.signup, name='signup'), |
| | + re_path(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), |
| | + re_path(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'), |
| | ] |
views.py
- เพิ่ม function signup สำหรับสมัคร id password ของ website (build-in ทำตาม
link)
| | @@ -1,5 +1,7 @@ |
| | from django.shortcuts import redirect, render, HttpResponseRedirect |
| | from django.urls import reverse |
| | +from django.contrib.auth import login, authenticate |
| | +from django.contrib.auth.forms import UserCreationForm |
| | from wordbucket.models import Word, Explanation, Like_and_dislike |
| | import string |
| | |
| @@ -90,3 +92,19 @@ def vote_dislike(request, explanation_id): |
| | votesdislike.votes_dislike += 1 |
| | votesdislike.save() |
| | return HttpResponseRedirect(reverse('wordbucket:detail', args=(explanation_.word.id,))) |
| | + |
| | +# auth. part |
| | + |
| | +def signup(request): |
| | + if request.method == 'POST': |
| | + form = UserCreationForm(request.POST) |
| | + if form.is_valid(): |
| | + form.save() |
| | + username = form.cleaned_data.get('username') |
| | + raw_password = form.cleaned_data.get('password1') |
| | + user = authenticate(username=username, password=raw_password) |
| | + login(request, user) |
| | + return redirect('/') |
| | + else: |
| | + form = UserCreationForm() |
| | + return render(request, 'signup.html', {'form': form}) |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น