NoReverseMatch at / accounts / login / form动作查询?

时间:2016-09-11 17:58:54

标签: python django

我试图学习django,但我无法在任何地方找到答案,或者至少不是简单的说法!我试图在我的项目中实现包含的登录视图,并且我尝试使用文档使用的示例html,我得到了这个错误。

Template error:
In template  
C:\Users\Owner\Documents\django\mysite\templates\registration\login.html, error at line 14
Reverse for 'login' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []   4 : 
5 : {% if next %}
6 :     {% if user.is_authenticated %}
7 :     <p>Your account doesn't have access to this page. To proceed,
8 :     please login with an account that has access.</p>
9 :     {% else %}
10 :     <p>Please login to see this page.</p>
11 :     {% endif %}
12 : {% endif %}
13 : 
14 : <form method="post" action=" {% url 'login' %} ">
15 : {% csrf_token %}
16 : <table>
17 : <tr>
18 :     <td>{{ form.username.label_tag }}</td>
19 :     <td>{{ form.username }}</td>
20 : </tr>
21 : <tr>
22 :     <td>{{ form.password.label_tag }}</td>
23 :     <td>{{ form.password }}</td>
24 : </tr>

我不明白的是“Action =”{%url'登录'%}“实际上做了什么?我无法找到确切地寻找它的确切答案。这是我的blog / views.py文件

from django.shortcuts import render
from django.views import generic
from .models import Post
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.http import request, HttpResponse
from django.template import loader
 # Create your views here.
class ListViews(generic.ListView):
    template_name= 'blog/list.html'
    context_object_name= 'latest_blog_posts'

    def get_queryset(self):
        return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[:5]

@login_required
def DetailView(request,pk):
    model = Post
    data_list = Post.objects.get(id=pk)
    template = loader.get_template('blog/detail.html')
    context = {
        'post':data_list,
    }
    return HttpResponse(template.render(context,request))
#def get_queryset(self):
#return Post.objects.filter(published_date__lte=timezone.now())

这是我的mysite / urls.py文件:

from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views

urlpatterns = [
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', admin.site.urls),
url(r'^accounts/login/$', auth_views.login),
]

并且我已经在表单的操作字段中尝试了帐户/登录,这会产生相同的错误。任何想法将非常感谢!谢谢!

1 个答案:

答案 0 :(得分:0)

Action =“{%url'login'%}:这意味着当您提交表单时,它会让您在urls.py中搜索名为'login'的URL并执行视图。但是没有任何名为login的内容在你的urls.py.you可以尝试编辑urls.py的最后一行:         url(r'^ accounts / login / $',auth_views.login,name ='login'), 或编辑模板文件:         &LT; form method =“post”action =“。”&gt;