第一次发帖,请原谅任何菜鸟——我正在尝试创建一个多页产品注册表单。我可以获得要显示的表单和提交按钮,但我找不到任何我的 urls.py 文件的 URL 模式示例。我发现另一个类似的问题有以下建议:
“您需要编写一个视图来处理您的请求。(这样做)您需要编辑 urls.py 以将您的测验 url 映射到 views.py 中的函数(尝试,但失败)。所以当一个带有该测验 url 的请求时来 Django 应用该视图功能。
当我将用户重定向到新的 url 是问题所在。我似乎找不到下一个模式应该是什么的例子。这是我的 urls.py 代码(到目前为止,index.html 和 details.html 是我的模板):
url(r'^signup/$', 'signup.views.select_product', name='select_product'),
url(r'^signup/(?P<product_id>\d+)/$', 'signup.views.subscriber_signup', name='subscriber_signup'),
#...
这是我的视图代码:
def select_product(request):
title = "get yourself an e-edition. wurd."
pform = ProductForm(request.POST)
if request.method == 'POST': # If the form has been submitted...
pform = ProductForm(request.POST) # A form bound to the POST data
if pform.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('signup/index.html') # Redirect after POST
else:
form = ProductForm() # An unbound form
return render_to_response('signup/index.html', {'title': title, 'pform': pform}, context_instance=RequestContext(request))
def subscriber_signup(request, product_id):
signup = Product.objects.get(id=product_id)
title = "get yourself an e-edition. wurd."
sform = SubscriberForm(request.POST)
if request.method == 'POST': # If the form has been submitted...
sform = SubscriberForm(request.POST) # A form bound to the POST data
if sform.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('signup/detail.html') # Redirect after POST
else:
sform = SubscriberForm() # An unbound form
return render_to_response('signup/detail.html', {'title': title, 'sform': sform, 'signup': signup,}, context_instance=RequestContext(request))