我正在通過看Django教程來坐在網上。 我得到了一個錯誤:NoReverseMatch at/polls/
NoReverseMatch at /polls/ Reverse for 'index' with no arguments not found. 1 pattern(s) tried: ['$/'].
回溯說
Traceback:
File "/Users/XXX/django/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/Users/XXX/django/django/core/handlers/base.py" in _get_response
130. response = self.process_exception_by_middleware(e, request)
File "/Users/XXX/django/django/core/handlers/base.py" in _get_response
128. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/XXX/djangostudy/polls/views.py" in index
23. 'questions': Question.objects.all(),
File "/Users/XXX/django/django/shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/XXX/django/django/template/loader.py" in render_to_string
62. return template.render(context, request)
File "/Users/XXX/django/django/template/backends/django.py" in render
61. return self.template.render(context)
File "/Users/XXX/django/django/template/base.py" in render
175. return self._render(context)
File "/Users/XXX/django/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/Users/XXX/django/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/Users/XXX/django/django/template/base.py" in render_annotated
910. return self.render(context)
File "/Users/XXX/django/django/template/loader_tags.py" in render
155. return compiled_parent._render(context)
File "/Users/XXX/django/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/Users/XXX/django/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/Users/XXX/django/django/template/base.py" in render_annotated
910. return self.render(context)
File "/Users/XXX/django/django/template/loader_tags.py" in render
155. return compiled_parent._render(context)
File "/Users/XXX/django/django/template/base.py" in _render
167. return self.nodelist.render(context)
File "/Users/XXX/django/django/template/base.py" in render
943. bit = node.render_annotated(context)
File "/Users/XXX/django/django/template/base.py" in render_annotated
910. return self.render(context)
File "/Users/XXX/django/django/template/defaulttags.py" in render
447. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/Users/XXX/django/django/urls/base.py" in reverse
87. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/XXX/django/django/urls/resolvers.py" in _reverse_with_prefix
488. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /polls/
Exception Value: Reverse for 'index' with no arguments not found. 1 pattern(s) tried: ['$/']
我base.html文件是在爲同父的應用寫道就像
{% load staticfiles %}
{% load bootstrap3 %}
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>Starter Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="static/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 50px;
}
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{% url 'index' %}">Tutorial</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="{% block nav_polls %}{% endblock %}"><a href="{% url 'polls:index' %}">polls</a></li>
<li class=""><a href="{% url 'admin:index' %}">admin</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
{% bootstrap_messages messages %}
{% block contents %}{% endblock %}
</div><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
</body>
</html>
父母目錄的urls.py是
from django.conf.urls import include,url
from django.contrib import admin
from polls.views import index
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/',include('polls.urls')),
url(r'^$/',index,name='index'),
]
孩子應用程序,其名稱是投票就像是
from django.conf.urls import url
from django.views.generic import TemplateView
from . import views
app_name="polls"
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'(?P<pk>\d+)/$', views.detail, name='poll_detail'),
url(r'(?P<pk>\d+)/vote$', views.vote, name='poll_vote'),
url(r'(?P<pk>\d+)/results$', views.results, name='poll_results'),
url(r'^$',TemplateView.as_view(template_name='app/index.html'),name='app_index'),
url(r'^form$', views.form_test),
]
我真的不明白爲什麼這個錯誤happens.Is指定"{% url 'index' %}"
錯誤的方式urls.py?我怎樣才能解決這個問題?
我views.py就像
from django.shortcuts import render
from django.urls import reverse_lazy
from django.utils.html import mark_safe
from .models import Question
from django.http import HttpResponse
from django.shortcuts import Http404
from django.shortcuts import get_object_or_404,redirect
from .models import Choice
from django.views.generic import TemplateView
from django.views.generic import DetailView
from django.views.generic import ListView
from .forms import MyForm
from .forms import VoteForm
from django.views.generic import FormView
from django.views.generic.detail import SingleObjectMixin
from django.shortcuts import resolve_url
def index(request):
return render(request,'polls/index.html',{
'questions': Question.objects.all(),
})
def vote(request,pk):
question = get_object_or_404(Question,pk=pk)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError,Choice.DoesNotExist):
return render(request,'poll/detail.html',{
'question':question,
'error_message':"You didn't select a choice",
})
else:
selected_choice.votes += 1
selected_choice.save()
return redirect('index')
return redirect('poll_results', pk)
# pass
def results(request,pk):
obj = get_object_or_404(Question,pk=pk)
return render(request,'polls/results.html',{
'question':obj,
})
class FormTest(FormView):
form_class = MyForm
template_name = 'polls/form.html'
success_url = reverse_lazy('polls:index')
form_test = FormTest.as_view()
class Detail(SingleObjectMixin,FormView):
model = Question
form_class = VoteForm
context_object_name = 'question'
template_name = 'polls/detail.html'
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['question'] = self.object
return kwargs
def form_valid(self, form):
form.vote()
return super().form_valid(form)
def get_success_url(self):
return resolve_url('polls:results',self.kwargs['pk'])
detail = Detail.as_view()
我的目錄是 directory
url(r'^ $ /',index,name ='index'),這兩個url文件。我認爲這造成了問題 – Ananthu
@Ananthu thx,我刪除了每個url(r'^ $ /',index,name ='index'),但是它們都發生錯誤。我該如何解決這個問題? – user8504021
嘗試修復正則表達式的URL到這個:'url(r'^ $',index,name ='index'),'不帶'/' – neverwalkaloner