Note that there are some explanatory texts on larger screens.

plurals
  1. POBreak up several actions into multiple view functions
    text
    copied!<p>I have one large view function where a user can Add, Edit, Delete, and Update his education. I am currently doing this all in one view because I haven't yet learned how to split up views by function. Here is what I currently have -- </p> <p>I have a single URL pointing to the view -- </p> <pre><code>url(r'^profile/edit/education/$', 'views.edit_education', name='edit_education') </code></pre> <p>Here is my model/modelform -- </p> <pre><code>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',) </code></pre> <p>Here is my view -- </p> <pre><code>@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)) </code></pre> <p>And finally, my template -- </p> <pre><code>&lt;h3&gt;Edit education info for {{user.get_full_name}}&lt;/h3&gt; &lt;form action="." method="post"&gt; {% csrf_token %} {% if education %} {% for education in education %} &lt;p&gt;&lt;b&gt;{{ education.school }}&lt;/b&gt; {% if education.class_year %}{{ education.class_year|shorten_year}}, {% endif %} {{ education.degree}} &lt;input type="submit" name="edit_{{education.id}}" value='Edit' /&gt; &lt;input type="submit" name="delete_{{education.id}}" value="Delete" /&gt;&lt;/p&gt; {% endfor %} {% endif %} &lt;table&gt; &lt;input type="hidden" name="education_id" value="{{education_id}}" /&gt; &lt;tr&gt;&lt;td&gt;School:&lt;/td&gt;&lt;td&gt;{{form.school}}{{form.school.errors}}&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Class Year&lt;/td&gt;&lt;td&gt;{{form.class_year}}{{form.class_year.errors}}&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Degree:&lt;/td&gt;&lt;td&gt;{{form.degree}}{{form.degree.errors}}&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;{{form.non_field_errors}}&lt;/tr&gt; &lt;/table&gt; {% if not edit %} &lt;p&gt;&lt;input type="submit" name="add" value="Add School" &gt;&lt;/p&gt; {% else %} &lt;p&gt;&lt;input type="submit" name="save" value="Save Changes" &gt; &lt;input type="submit" name="cancel" value="Cancel" &gt;&lt;/p&gt; {% endif %} &lt;/form&gt; </code></pre> <p>And the end is here. How would I separate one of these actions in the view into separate view functions using separate URLs? One or two examples would be more than enough. Thank you very much for your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload