Anjaxs

python编程:从入门到实践Django2.1中遇到的问题

第一个问题

在《python编程:从入门到实践》中,第18.3.1映射URL小节中的代码:

from django.conf.urls import include, url
from django.contrib import admin
    urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
]

由于在diango2.1版本中使用上面方法会报错,应该改成如下

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls', 'learning_logs')),
]

include的第2个参数为应用程序名称,在对应的learning_logs/urls.py中要添加app_name这个变量,并赋值为learning_logs,这个相当于一个命名空间,以后访问learning_logs这个应用程序中的路由需添加前缀,比如

HttpResponseRedirect(reverse(‘learning_logs:topics’))

from django.urls import path
from . import views

app_name = 'learning_logs'

urlpatterns = [
    path('', views.index, name='index'),   # 主页
    path('topics', views.topics, name='topics'),
    path('topic/<int:topic_id>', views.topic, name='topic'),
    path('new_topic/', views.new_topic, name='new_topic'),

    path('new_entry/<int:topic_id>', views.new_entry, name='new_entry'),
    path('edit_entry/<int:entry_id>', views.edit_entry, name='edit_entry'),
]

文档https://docs.djangoproject.com/en/2.1/topics/http/urls/
基于函数 views
1. 导入views: from my_app import views
2. 添加URL到urlpatterns: path(”, views.home, name=’home’)
基于类的views
1. 导入类: from other_app.views import Home
2. 添加URL到urlpatterns: path(”, Home.as_view(), name=’home’)
添加其他URL配置文件
1. 导入include()函数: from django.urls import include, path
2. 添加URL到urlpatterns: path(‘blog/’, include(‘blog.urls’))

第二个问题

在Django2.1中django.contrib.auth.views.login已经被移除

解决办法:

users/urls.py

from django.urls import path
from . import views

app_name = 'users'

urlpatterns = [
    path('login/', views.login_view, name='login'),
    path('logout/', views.logout_view, name='logout'),
    path('register/', views.register, name='register'),
]

users/views.py

def login_view(request):
    form = AuthenticationForm()
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse('learning_logs:index'))
        else:
            error = "Your username and password didn't match. Please try again."
            return render(request, 'users/login.html', {'error': error, 'form': form})
    else:
        return render(request, 'users/login.html', {'form': form})

users/templates/users/login.html

{% extends "learning_logs/base.html" %}
{% load bootstrap3 %}

{% block header %}
  <h2>Log in to your account.</h2>
{% endblock header %}

{% block content %}
  <form method="post" action="{% url 'users:login' %}" class="form">
    {% csrf_token %}
    {% bootstrap_form form %}
    {% buttons %}
      <button name="submit" class="btn btn-primary">log in</button>
    {% endbuttons %}
    <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
</form>
{% endblock content %}

本书的课后习题自己做的答案:https://download.csdn.net/download/qq_33473088/10614352

 

 

 

码字很辛苦,转载请注明来自Anjaxs博客《python编程:从入门到实践Django2.1中遇到的问题》

评论

  1. 一个小白 #1

    感谢博主救我! 😥 折腾一晚上了!神坑啊!

    回复
    2018-10-26