如何解决将几个动作分解为多个视图函数
| 我有一个大视图功能,用户可以在其中添加,编辑,删除和更新他的知识。我目前正在一个视图中进行所有操作,因为我还没有学会如何按功能拆分视图。这是我目前拥有的- 我有一个指向视图的网址-url(r\'^profile/edit/education/$\',\'views.edit_education\',name=\'edit_education\')
这是我的模型/模型形式-
class Education(models.Model):
school = models.CharField(max_length=100)
class_year = models.IntegerField(max_length=4,blank=True,null=True,choices=YEAR)
degree = models.CharField(max_length=100,blank=True)
user = models.ForeignKey(\'UserProfile\')
class EducationForm(ModelForm):
class Meta:
model = Education
exclude = (\'user\',)
这是我的看法-
@login_required
def edit_education(request,edit=0):
\"\"\"
In the edit profile page,allows a user to edit his education
and add multiple school entries.
\"\"\"
profile = request.user.get_profile()
education = profile.education_set.order_by(\'-class_year\') # for the template. display all eduation entries
# unindented for legibility
if request.method == \'POST\':
if \'Add School\' in request.POST.values():
form = EducationForm(data=request.POST,request=request) # passing request to form to do validation based on request.user
if form.is_valid():
new_education = form.save(commit=False)
new_education.user = profile
new_education.save()
return redirect(\'edit_education\')
if \'Delete\' in request.POST.values():
for education_id in [key[7:] for key,value in request.POST.iteritems() if key.startswith(\'delete\')]:
Education.objects.get(id=education_id).delete()
return redirect(\'edit_education\')
if \'Edit\' in request.POST.values():
for education_id in [key[5:] for key,value in request.POST.iteritems() if value == \'Edit\' and key.startswith(\'edit\')]:
edit = 1
school_object = Education.objects.get(id = education_id)
form = EducationForm(instance = school_object,request=request)
return render_to_response(\'userprofile/edit_education.html\',{\'form\': form,\'education\':education,\'edit\': edit,\'education_id\': education_id},context_instance=RequestContext(request))
if \'Cancel\' in request.POST.values():
return redirect(\'edit_education\')
if \'Save Changes\' in request.POST.values():
form = EducationForm(request.POST,request=request,edit=1)
if form.is_valid():
Education.objects.get(id=request.POST[\'education_id\']).delete() # is there a way to update instead of delete and re-add?
new_education = form.save(commit=False)
new_education.user = profile
new_education.save()
return redirect(\'edit_education\')
else:
form = EducationForm(request=request)
return render_to_response(\'userprofile/edit_education.html\',\'education\': education,},context_instance=RequestContext(request))
最后,我的模板-
<h3>Edit education info for {{user.get_full_name}}</h3>
<form action=\".\" method=\"post\"> {% csrf_token %}
{% if education %}
{% for education in education %}
<p><b>{{ education.school }}</b> {% if education.class_year %}{{ education.class_year|shorten_year}},{% endif %} {{ education.degree}}
<input type=\"submit\" name=\"edit_{{education.id}}\" value=\'Edit\' />
<input type=\"submit\" name=\"delete_{{education.id}}\" value=\"Delete\" /></p>
{% endfor %}
{% endif %}
<table>
<input type=\"hidden\" name=\"education_id\" value=\"{{education_id}}\" />
<tr><td>School:</td><td>{{form.school}}{{form.school.errors}}</td></tr>
<tr><td>Class Year</td><td>{{form.class_year}}{{form.class_year.errors}}</td></tr>
<tr><td>Degree:</td><td>{{form.degree}}{{form.degree.errors}}</td></tr>
<tr>{{form.non_field_errors}}</tr>
</table>
{% if not edit %}
<p><input type=\"submit\" name=\"add\" value=\"Add School\" ></p>
{% else %}
<p><input type=\"submit\" name=\"save\" value=\"Save Changes\" >
<input type=\"submit\" name=\"cancel\" value=\"Cancel\" ></p>
{% endif %}
</form>
结局就在这里。如何使用单独的URL将视图中的这些操作之一分解为单独的视图函数?一个或两个例子就足够了。非常感谢您的帮助。
解决方法
一些想法:
您可以将一个大的html表单元素拆分为大块
您可以使用AJAX提交处理程序根据按下的提交按钮更改URL
您可以按照用户
Cerales
的建议进行操作,但是可以直接调用add_school()
和其他方法来代替丢失POST数据的重定向,还可以将操作的字典映射映射到其处理程序:action_map = {\'Add School\': add_school,...}
-这将消除条件链
您可以使用基于类的视图,该视图基本上是#3的基于类的版本。 Django文档提供了基于类的通用视图
如果您愿意,我可以详细说明这些想法。
-
编辑:
通过评论回答您的问题:
from django.views.generic.base import View
class MySchoolView(View):
def post(self,request,*kargs,**kwargs):
if \'Add School\' in request.POST:
return self.add_school(request,**kwargs)
# (...)
def add_school(self,**kwargs):
# (...)
然后在urls.py中:
(r\'^schools/add/$\',MySchoolView.as_view())
请注意,以上内容未经测试,因此可能需要进行一些调整。 View
类源代码在这里。
, 您可以通过两种方法来执行此操作。
这可能是您观点的一部分:
if request.method == \'POST\':
if \'Add School\' in request.POST.values():
return HttpResponseRedirect(\'/add_school/\')
然后,这可能是另一个视图的一部分,对应于ѭ11corresponding url:
def add_school(request):
if request.method==\'POST\':
form = EducationForm(data=request.POST,request=request) # passing request to form to do validation based on request.user
if form.is_valid():
new_education = form.save(commit=False)
new_education.user = profile
new_education.save()
return redirect(\'edit_education\')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。