new_join.save()之后HttpResponseRedirect不起作用

时间:2015-04-15 07:56:35

标签: django

我试图制作简单的申请表。成功发送表单后,我需要将用户重定向到“谢谢”页面。但是在用户点击“发送”按钮后 - 没有任何反应。只需重新加载表单页面,也无需进行表单清理。

表单位于“呼叫”页面,重定向需要“确认”页面...

所以,任务是:用户填写“呼叫”页面上的表格,点击“发送”按钮后,进入“确认”页面。

我的模特:

# -*- coding: utf-8 -*- 

from django.db import models
from django.forms import ModelForm

# Create your models here.

class Join(models.Model):
    class Meta():
        db_table = 'expert_request'

user_expert = models.CharField(max_length=100)
user_name = models.CharField(max_length=100)
user_cost = models.CharField(max_length=100)
user_email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)

def __unicode__(self):
    return self.user_email

我的“forms.py”:

from django import forms
from userform.models import Join

class JoinForm(forms.ModelForm):
    class Meta:
        model = Join

这是我的“views.py”:

from django.shortcuts import render
from django.shortcuts import HttpResponseRedirect, Http404

# Create your views here.
from userform.forms import JoinForm
from userform.models import Join

def userform(request):

    form = JoinForm(request.POST or None)
    if form.is_valid():
        new_join = form.save(commit=False)
        new_join.save()
        HttpResponseRedirect('/confirm/')

    context = {"form": form}
    template = "userform.html"
    return render(request, template, context)

    def confirm(request):

    return render(request, 'confirm.html')

这是我的网址:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),

    (r'^tinymce/', include('tinymce.urls')),

    url(r'^$', 'expert.views.index', name='index'),
    url(r'^(\d+)/?$', 'expert.views.index'),

    url(r'^call/$', 'userform.views.userform', name='call'),
    url(r'^confirm/$', 'userform.views.confirm', name='confirm'),

)

我的模板“userform.html”:

{% load staticfiles %}


<form style="position:relative" method="POST" action="">{% csrf_token %}
{{ form.user_expert }}
<p style="position:absolute; top:50px; left:20px; color:#FF0000;">{{     form.user_expert.errors.as_text }}</p>
{{ form.user_name }}
<p style="position:absolute; top:182px; left:20px; color:#FF0000;">{{ form.user_name.errors.as_text }}</p>
{{ form.user_cost }}
<p style="position:absolute; top:50px; left:380px; color:#FF0000;">{{ form.user_cost.errors.as_text }}</p>
{{ form.user_email }}
<p style="position:absolute; top:182px; left:380px; color:#FF0000;">{{ form.user_email.errors.as_text }}</p>
<button type="submit">Send</button>
<div class="clear"></div>
</form>

1 个答案:

答案 0 :(得分:0)

也许您忘记了return查看功能中的userform语句?修复它的开头:

def userform(request):

    form = JoinForm(request.POST or None)
    if form.is_valid():
        new_join = form.save(commit=False)
        new_join.save()
        return HttpResponseRedirect('/confirm/')