使用方法改进Django视图 [英] Improve Django View with Methods
问题描述
我正在寻求改进Django Web应用程序,主要是项目脚本.我多次使用相同的Python代码,并且想简化我的 views.py
文件.
I'm looking to improve my Django web application and mainly project scripts.
I'm using many times the same Python code and I would like to simplify my views.py
file.
例如,我有几次这部分:
For example, I have several times this part :
if request.method == 'POST':
form = FormBase(request.POST or None, request.FILES or None)
if form.is_valid() :
post = form.save()
return HttpResponseRedirect(reverse('ReverseTemplate', kwargs={'id': post.id}))
else :
form = FormBase()
form.fields['Utilisateur'].initial = request.user.last_name + " " + request.user.first_name
因此,我不想设置/复制该部分到我的其他视图功能中,而是设置:
So, instead of copy/past this part in my different view function, I would like to set :
def DjangoFormID(request, FormBase, ReverseTemplate) :
if request.method == 'POST':
form = FormBase(request.POST or None, request.FILES or None)
if form.is_valid() :
post = form.save()
return HttpResponseRedirect(reverse('ReverseTemplate', kwargs={'id': post.id}))
else :
form = FormBase()
form.fields['Utilisateur'].initial = request.user.last_name + " " + request.user.first_name
然后在我看来调用此函数:
And call this function in my view :
DjangoFormID(request, IndividuFormulaire, IndividuResume)
我想知道我的程序是否正常?是比我做的更好的编程方式吗?
I would like to know if my process is fine ? Is it a better way to programming rather than what I've done ?
然后,我遇到此错误:未定义名称'IndividuResume'
Then, I'm getting this error : name 'IndividuResume' is not defined
如何在方法中编写Django模板?
How I could write my Django template inside my method ?
谢谢
推荐答案
错误似乎是因为'IndividuResume'
应该是字符串,而不是变量名.
It looks like the error is because 'IndividuResume'
should be a string, not a variable name.
DjangoFormID(request, IndividuFormulaire, 'IndividuResume')
在模板内部,应使用变量 ReverseTemplate
,而不是硬编码字符串'ReverseTemplate'
Inside your template, you should use the variable ReverseTemplate
, not the hardcoded string 'ReverseTemplate'
return HttpResponseRedirect(reverse(ReverseTemplate, kwargs={'id': post.id}))
在每个视图中包含相同的样板的优点在于,很容易跟踪正在发生的事情.如果您担心重复,我会考虑查看基于类的视图,因为它们是为这种用例设计的.我会避免定义自己的 DjangoFormID
-如果我是您项目的新手,我不知道 DjangoFormID(request,IndividuFormulaire,'IndividuResume')
做了什么,所以会放慢我的速度.
The advantage of including the same boilerplate in each view is that it's very easy to follow what is going on. If you are worried about the repetition, I would consider looking at class based views, because they are designed for this kind of use case. I would avoid defining your own DjangoFormID
-- if I was new to your project I would not know what DjangoFormID(request, IndividuFormulaire, 'IndividuResume')
did, so it would slow me down.
如果您确实坚持使用 DjangoFormID
方法,那么这里有一些建议:
If you do stick with your DjangoFormID
method then here's a couple of recommendations:
- 确保它总是返回响应.目前,它会返回有效表单的重定向响应,其余时间返回
None
,这会引起问题. - 当我看到
DjangoFormID
时,我认为它是一个类,而不是一个函数.将其重命名为django_form_id
.
- Make sure it always returns a response. At the moment, it returns a redirect response for a valid form, and
None
the rest of the time, which is going to cause problems. - when I see
DjangoFormID
I assume it's a class, not a function. Rename it todjango_form_id
.
这篇关于使用方法改进Django视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!