将多个动作分解为多个视图功能

时间:2011-06-15 04:56:34

标签: python django django-models django-forms django-views

我有一个大视图功能,用户可以在其中添加,编辑,删除和更新他的教育。我目前正在一个视图中执行此操作,因为我还没有学习如何按功能拆分视图。这是我目前的情况 -

我有一个指向视图的网址 -

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', {'form': form, '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将视图中的其中一个操作分离为单独的视图函数?一两个例子就足够了。非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。

这可能是您观点的一部分:

if request.method == 'POST':

    if 'Add School' in request.POST.values():
        return HttpResponseRedirect('/add_school/')

然后,这可能是另一个视图的一部分,与/add_school/ 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')

答案 1 :(得分:1)

一些想法:

  1. 您可以将一个大的html表单元素拆分为块
  2. 您可以使用AJAX提交处理程序根据按下的提交按钮
  3. 更改URL
  4. 您可以执行用户Cerales建议的内容,但不是重定向哪个丢失POST数据,您只需调用add_school()和其他方法,可能还有映射到其处理程序的操作的字典映射:{{ 1}} - 这将消除条件链
  5. 您可以使用基于类的视图,它基本上是#3的基于类的版本。基于类的通用视图的Django文档是here
  6. 如果你愿意,我可以详细说明这些想法。

    -

    编辑:

    通过评论回答您的问题:

    action_map = {'Add School': add_school, ...}

    然后在urls.py中:

    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, *kargs, **kwargs)
            # (...)
        def add_school(self, request, *kargs, **kwargs):
            # (...)
    

    请注意,上述内容未经过测试,因此可能需要进行一些调整才能正常工作。 (r'^schools/add/$', MySchoolView.as_view()) 类源代码为here