我正在尝试获取我的初学者Django应用程序http://chishenma.herokuapp.com/的索引页面,以允许用户使用他们所在的城市和电子邮件地址注册加入等待名单。我直接从http://chishenma.herokuapp.com/register/的注册页面(减去几个字段)中提取了代码,但即使注册页面正在工作,索引页面上的注册也不起作用。由于某些原因,URL变成了/waiting_list,但实际上并没有注册(我在admin中进行了两次检查,可以找到来自/register页面的注册),页面上或控制台上都没有错误。下面是views.py中索引和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:
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发布于 2014-05-25 00:20:16
这一行可能是您的问题所在:
<form action="{% url 'waiting_list' %}" method="post">当提交表单时,POST被发送到waiting_list方法,该方法完全没有POST处理(来自您粘贴的内容)。相反,所有的POST处理都是基于索引的。
尝试将其更改为<form action="." method="post">,或者更具体地说,更改为<form action="{% url 'home' %}" method="post">。两者都应该将帖子发送到index,后者完成注册。
https://stackoverflow.com/questions/23842880
复制相似问题