0

基础:我正在尝试使用新的唯一 url 显示表单信息以及提交时的视图。表单当前正在保存到数据库,但不会重定向到新的 url 或视图。下面我发布了views.py 和urls.py。如果您需要查看其他内容,请告诉我。

注意:这是基于 ModelForm 的,我是 Django 的初学者。

提前致谢!

网址.py

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

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^letter/testing/$', 'letter.views.letter'),
url(r'^letter/testing/(?P<pk>\d+)/$','letter.views.submission'),

# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),

)

视图.py

# Create your views here.
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from letter.forms import LetterForm
from letter.models import Letter

def submission(request, pk):
    submission = get_object_or_404(Letter, pk=int(pk))
    return render_to_response('post.html', {'submission':submission})

def letter(request):
if request.method == "POST":
    letter = LetterForm(request.POST)

    if letter.is_valid():
        try: 
            new_letter = letter.save()
            return HttpResponseRedirect('/letter/testing/%d/' % new_letter.pk)
        except:
            pass        

else:
    letter = LetterForm()

return render_to_response('letter.html',
        {'letter':letter},
        context_instance=RequestContext(request))

解答: 需要删除 if letter.is_valid() 下的 try/except 参数

谢谢@mattg!

4

1 回答 1

0

需要去掉下的tryexcept参数if letter.is_valid()

于 2013-04-18T23:46:19.410 回答