博客基础_django入门_python从入门到实践_用户登陆、注销与注册

要求

  登陆、注销系统用户;注册新用户

  urls.py  views.py   html

  login.html  logout.html   regiser.html

截图loading

 

代码

users

 1 # learning_log/   
 2     python manage.py startapp users
 3     ls users
 4 
 5 # settings.py     learning_log/ users/
 6     --snip
 7     INSTALL_APPS=[
 8         'users',
 9     ]
10 
11 # urls.py    users/
12 urlpatterns=[
13     ...
14     url('^users/$', 'users.urls', namespace='urls')
15 ]
View Code

 

urls.py  users/

 1 from django.conf.urls import urls
 2 from django.contrib.auth.views import login
 3 
 4 from . import views
 5 
 6 urlpatterns=[
 7     url(r'^login/$', login, {'template_name':'users/login.html'}, name='login'),
 8     url(r'^logout/$', views.logout_view, name='logout'),
 9     url(r'^register/$', views.register, name='register')
10 ]
View Code

 

views.py   users/

 1 from django.shortcuts import render
 2 from django.http import HttpResponseRedirect 
 3 from django.core.urlresolvers import reverse
 4 from django.contrib.auth import login, logout, authenticate
 5 from django.contrib.auth.forms import UserCreationForm
 6 
 7 def logout_view(request):
 8     logout(request)
 9     return HttpResponseRedirect(reverse('learning_logs:index'))
10 
11 def register(request):
12     if request.method != 'POST':
13         form = UserCreationForm()
14     else:
15         form = UserCreationForm(data=request.POST)
16         if form.is_valid():
17             new_user = form.save()
18             authenticated_user = authenticate(username=new_user.username, password=request.POST['password1'])
19             login(request, authenticated_user)
20             return HttpResponseRedirect(reverse('learning_logs:index'))
21     context = {'form':form}
22     return render(request, 'users/register.html', context)
View Code

 

login.html  users/templates/users/

 1 {% extends 'learning_logs/base.html' %}
 2 
 3 {% block content %}
 4     {% if form.erros %}
 5         <p>Your username and password didn't match, please try again.</p>
 6     {% endif %}
 7         <form action="{% url 'users:login' %}">
 8             {% csrf_token %}
 9             {{ form.as_p }}
10             <button name="submit">login in</button>
11             <input type="hidden" name="next" value="{% url 'learning_log:index' %}" />
12         </form>
13 {% endblock content %}
View Code

 

register.html  users/templates/users/

 1 {% extends 'learning_logs/base.html' %}
 2 
 3 {% block content %}
 4     {% if form.erros %}
 5         <p>Your username and password didn't match, please try again.</p>
 6     {% endif %}
 7         <form action="{% url 'users:login' %}">
 8             {% csrf_token %}
 9             {{ form.as_p }}
10             <button name="submit">login in</button>
11             <input type="hidden" name="next" value="{% url 'learning_log:index' %}" />
12         </form>
13 {% endblock content %}
View Code

 

base.html  learning_logs/templates/learning_logs/

View Code

 

posted @ 2018-10-23 00:17  phappiness  阅读(405)  评论(0编辑  收藏  举报