我的django views.py文件中有两个函数,它们给我带来了问题。 Django显示在else行有一个语法错误,但我看不到它附近的任何问题。(所有必需的函数等都是导入的) 我想要一些帮助
@login_required
def add_post(request, forumslug, threadslug):
if request.method=='POST':
form = PostForm(request.POST)
if form.is_valid():
user = request.user
title = form.cleaned_data['title']
body = form.closed_data['body']
thread = get_object_or_404(Thread, slug=threadslug)
forum = get_object_or_404(Forum, slug=forumslug)
post = Post.objects.create(title=title, body=body, creator=user, thread=thread)
url = '/%s/%s/' % (forumslug, slugify(title))
return HttpResponseRedirect(url)
else:
form = PostForm()
variables = RequestContext(request, {'form': form})
return render_to_response('forum/new_post.html', variables)
@login_required
def add_thread(request, forumslug):
if request.method=="POST":
form = PostForm(request.POST)
if form.is_valid():
user = request.user
forum = get_object_or_404(Forum, slug=forumslug)
title = form.cleaned_data['title']
body = form.cleaned_data['body']
thread = Thread.objects.create(title=title, slug=slugify(title), forum=forum, creator=user, body=body)
url = '/%s/%s' % (forumslug, slugify(title))
return HttpResponseRedirect(url)
else:
form = PostForm()
variables = RequestConext(request, {'form': form})
return render_to_response('forum/new_thread.html', variables)
答案 0 :(得分:4)
行url = '/%s/%s/' % (forumslug, slugify(title)
实际上缺少一个结束括号,确定不会为else
以上的行引发错误?