views.py
from django.views.generic.simple import direct_to_template
def post_tweet(request, tweet_id=None):
tweet = None
if tweet_id:
tweet = get_object_or_404(Tweet, id=tweet_id)
if request.method == 'POST':
form = TweetForm(request.POST, instance=tweet)
if form.is_valid():
new_tweet = form.save(commit=False)
new_tweet.state = 'pending'
new_tweet.save()
send_review_email()
return HttpResponseRedirect('/post/thankyou/')
else:
form = TweetForm(instance=tweet)
return direct_to_template(request,'post_tweet.html',{'form': form})
urls.py
urlpatterns = patterns('',
url(r'^$', 'post_tweet'),
答案 0 :(得分:1)
基于函数的通用视图被从Django 1.3开始的基于类的视图所取代。 direct_to_template
的替换为TemplateView
。或者您可以将其切换为使用具有相同签名的render
快捷方式。
from django.shortcuts import render
...
def post_tweet(request, tweet_id=None):
...
return render(request,'post_tweet.html',{'form': form})