Reverse for 'vote' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['vote/(?P\d+)/$'
vote2 代码:
<p>please sigh your name<p>
<form action="{% url 'votes:vote2' %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">done</button>
</form>
<p>
<a href="{% url 'votes:vote' %}">if you had press 'done', press here</a>
</p>
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^vote2/$',views.vote2,name='vote2'),
url(r'^thanks/$',views.thanks,name='thanks'),
url(r'^vote/(?P<name_id>\d+)/$', views.vote,name='vote'),
url(r'^vote3/(?P<name_id>\d+)/$', views.vote3,name='vote3'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Name
from .forms import FormName, FormR
# Create your views here.
def index(request):
return render(request, 'votes/index.html')
def vote(request, name_id):
name = Name.objects.get(id=name_id)
context = {'name': name}
if 1==1:
return HttpResponseRedirect(reverse('vote:vote3', args=[name_id]))
return render(request, 'vote/vote.html', context)
def vote2(request):
if request.method != 'POST':
form = FormName()
else:
form = FormName(request.POST)
print(form)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('votes/vote'))
context = { 'form': form}
return render(request, 'votes/vote2.html', context)
def vote3(request, name_id):
name = Name.objects.get(id=name_id)
if request.method != 'POST':
form = FormR()
else:
form = FormR(data=request.POST)
if form.is_valid():
R = form.save(commit=False)
R.name = name
R.save()
return HttpResponseRedirect(reverse('votes/thanks', args=[name_id]))
context = {'name': name,'form': form}
return render(request, 'votes/vote3.html', context)
def thanks(request):
return render(request, 'votes/thanks.html')
vote.html
<p>
<a href="{% url 'votes:vote3' name.id %}">click me!!!</a>
</p>
有什么问题吗?