我正在尝试为我的初学者的Django应用http://chishenma.herokuapp.com/获取索引页面,以允许用户注册加入等待列表及其城市和电子邮件地址。我从http://chishenma.herokuapp.com/register/的注册页面(减去几个字段)中直接删除了代码,但即使注册页面正常工作,索引页面上的注册也无法正常工作。由于某种原因,URL更改为/ waiting_list,但实际上没有注册(我在管理员中进行了双重检查,可以在/ register页面中找到注册)并且页面或控制台中都没有错误。这是views.py中的index和waiting_list视图的代码:
def index(request):
# user_form = UserCreationForm()
city_form = UserCityForm()
waitlist_form = WaitlistForm()
if request.method == 'POST':
city_form = UserCityForm(request.POST)
waitlist_form = WaitlistForm(request.POST)
if city_form.is_valid() and waitlist_form.is_valid():
list_item, created = add_to_waitlist(waitlist_form.cleaned_data['email'])
return redirect(reverse('home'))
return render(request, "chishenma/index.html", {
'city_form': city_form,
'waitlist_form': waitlist_form,
})
def waiting_list(request):
return render(request, 'registration/waiting_list.html')
以下是forms.py:
中的表单class WaitlistForm(forms.Form):
email = forms.EmailField(max_length=35)
fields = ('user_email,')
class UserCityForm(forms.Form):
user_city = forms.CharField(max_length=25, required=True)
fields = ('user_city,')
这是index.html模板:
{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<div data-role="page" id="home">
<!-- Header -->
<div data-role="header" data-position="fixed" style="background-color:#EEEEEE;" data-tap-toggle="false">
<h1>Welcome to ChiShenMa!</h1>
</div><!-- /header -->
<div data-role="content">
<div id="user_area">
{% if user.is_authenticated %}
<p>Hello, {{user.username}}!</p>
{% else %}
<p>We can't wait to show you our app! Please fill in the information below to join our waiting list.</p>
{% endif %}
</div>
{% if not authenticated %}
<form action="{% url 'waiting_list' %}" method="post">
{{ city_form.as_p }}
{{ waitlist_form.as_p }}
<input type="submit" value="Join waiting list"/>
{% csrf_token %}
</form>
{% else %}
<a href="{% url 'logout' %}">Logout</a>
<!-- Footer -->
<div data-role="footer" data-theme="a" data-position="fixed" style="background-color:#EEEEEE;" data-tap-toggle="false">
<a href="{% url 'choose_category' %}" class="ui-btn ui-corner-all" style="background-color:#FFCC00; display:block; margin:5px;">Click here to find something to eat!</a>
</div><!-- /footer -->
{% endif %}
以下是waiting_list.html模板:
{% extends "base.html" %}
{% load staticfiles %}
{% block title %}Waiting list{% endblock %}
{% block content %}
<div data-role="page" id="waiting_list" data-theme="a">
<div data-role="header" data-position="fixed" style="background-color:#EEEEEE;" data-tap-toggle="false" data-add-back-btn="true">
<h1>Waiting list</h1>
</div><!-- /header -->
<h1>Thank you for signing up for our waiting list</h1>
{% endblock %}
来自urls.py的适用网址:
url(r'^$', 'chishenma_app.views.index', name='home'),
url(r'^waiting_list/$', 'chishenma_app.views.waiting_list', name='waiting_list'),
我在本地投放时控制台中的错误:
[24/May/2014 16:22:03] "GET / HTTP/1.1" 200 5663
[24/May/2014 16:22:12] "POST /waiting_list/ HTTP/1.1" 302 0
[24/May/2014 16:22:12] "GET / HTTP/1.1" 200 5663
我做错了什么?
编辑:
我将“data-ajax ='false'”添加到我的表单中,但同样的事情发生了,除了这次在终端我得到了这个:
[24/May/2014 16:46:46] "POST /waiting_list/ HTTP/1.1" 302 0
[24/May/2014 16:46:46] "GET / HTTP/1.1" 200 5681
答案 0 :(得分:0)
这一行可能是你的问题:
<form action="{% url 'waiting_list' %}" method="post">
提交表单后,POST将被发送到waiting_list方法,该方法根本没有POST处理(来自您粘贴的内容)。相反,所有POST处理都在索引上。
尝试将其更改为<form action="." method="post">
,或者更具体地说,<form action="{% url 'home' %}" method="post">
。两者都应该将POST发送到index
,这样就完成了注册。