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

Posted phappiness

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了博客基础_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 didnt 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 didnt 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

 

以上是关于博客基础_django入门_python从入门到实践_用户登陆注销与注册的主要内容,如果未能解决你的问题,请参考以下文章

Python自学从入门到就业之函数基础(小白必看)

python16_day15Django入门

Django入门4--admin

python学习_day69_django入门项目(老师表)

学习python之路_入门篇A

如何入门 Python 爬虫