我正在部署一个django应用程序,我有两种形式:一种用于评级,另一种用于主页中的联系。保存模型后,它们都重定向到主页。联系表单工作正常,但评级表保留了帖子网址,并没有保存模型,再次执行后,它保存。 例如,我在www.site.com,然后我从评级表发送一个帖子请求,表格将我重定向到www.site.com/rating/而不保存数据。然后,如果我从www.site.com/rating/发送相同的表格,则会保存数据并重定向到www.site.com/rating/rating/(404)。联系表格在同一过程中运行良好,我认为两者都相似。联系表格将我重定向到www.site.com,就像我想要的那样。我不知道为什么会这样。 urls.py
urlpatterns = i18n_patterns(
url(r'^admin/', admin.site.urls),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^', include('myapp.urls')),
prefix_default_language=False
)
的myapp / urls.py
urlpatterns = [
url(r'^contact/', views.contact, name='contact'),
url(r'^rating/', views.raiting, name='rating'),
url(r'^', views.index, name='home'),
]
的myapp / views.py
def contact(request):
if request.method != 'POST':
raise Http404('acceding from GET')
msg = ContactForm(request.POST)
if not msg.is_valid():
context = {
'contact_errors': msg.errors
}
return render(request, 'home.html', context)
msg.save()
return redirect('home')
def rating(request):
if request.method != 'POST':
raise Http404('acceding from GET')
msg_form = OpinionForm(request.POST)
if not msg_form.is_valid():
context = {
'rating_errors': msg_form.errors
}
return render(request, 'home.html', context)
msg = msg_form.save(commit=False)
if 'picture' in request.FILES:
msg.picture = request.FILES['picture']
msg.save()
return redirect('home')
home.html中的表单
<form action="{% url 'rating' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input name="name" type="text" class="form-control" placeholder="Your name" required>
<textarea name="text" id="" cols="30" rows="5" class="form-control" placeholder="Your comment" required></textarea>
<input name="picture" type="file" id="upload-photo">
<input type="submit" class="button" value="Send">
</form>
<form action="{% url 'contact' %}" method="post">
{% csrf_token %}
<input name="email" type="text" class="form-control" placeholder="Email" required>
<textarea name="message" id="" cols="30" rows="5" class="form-control" required placeholder="Your message"></textarea>
<input type="submit" class="button" value="Send">
</form>
答案 0 :(得分:0)
我猜这是一个错字,但请检查你的urls.py:
url(r'^rating/', views.raiting, name='rating'),
应该评分而不是raiting,对吧?