我有这个代码:
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
我在问什么:
- 我如何从 urls.py 调用这个方法(作为一个类的方法)?
- 有没有一种更简单的方法可以使方法“动态”或者你有什么 - 我的意思是,所以我不需要在那里放置所有那些丑陋的“if”块来检查已设置的内容并在必要时进行默认设置?