我有这段代码:
class StoryViewClass(ListView):
... some listview methods here for one set of urls
def saveStory(self,request,context_object_name,
template_name,
success_template):
if request.method == "POST":
form = StoryForm(request.POST)
form.user = request.user.id
if form.is_valid():
form.save()
if (success_template):
return render_to_response(success_template)
else:
return render_to_response('accounts/addStorySuccess.html')
else:
form = StoryForm()
if (context_object_name):
contextName = context_object_name
else:
contextName = 'form'
if (template_name):
return render_to_response(template_name,{contextName:form})
else :
return render_to_response('accounts/addStory.html',{contextName:form})
(这本身就是笨拙的,稍后会更多)
如何从我的网址中调用此内容?
我目前正在尝试这个:
url(r'^addStory/$',
StoryShowView.saveStory(
context_object_name='form',
template_name='accounts/addStory.html',
success_template='accounts/addStorySuccess.html'
)
),
但是django抱怨说
unbound method saveStory() must be called with StoryShowView instance as first argument (got nothing instead)
Request Method: POST
我在问什么:
答案 0 :(得分:2)
这不是你如何使用Django的基于类的视图。必须通过as_view()
方法从urls.py引用这些 。它们并不意味着每个类都有多个视图呈现方法 - 如果需要,最好将公共代码放在基类中并对其进行子类化。但在您的情况下,您可能只想更多地使用现有方法 - 例如,要确定要呈现的模板,您应该覆盖get_template_names()
。