0

Currently this code below allows me to show a Form and save to the database if I want to. But as I've seen in many tutorials like this I seem to find only Add ModelForm Tutorials and not Edit. How can I modify this code to also allow editing? Passing a ID or a Slug to it is based on a get.objets.get() #suggestion ID in this case.

Thanks In advance.

Model.py

from django.db import models

class Suggestion(models.Model):
     title = models.CharField(max_length=100)
     email = models.EmailField(blank=True)
     link = models.URLField(verify_exists=True,blank=True)
     description = models.TextField(blank=True)
     time_sensitive = models.BooleanField()
     approved = models.BooleanField()

   def __unicode__(self):
        return self.title

Form.py

from django import forms
from django.forms import ModelForm
from contact.models import Suggestion

class SuggestionForm(ModelForm):

   class Meta:
         model = Suggestion
         exclude = ('approved',)

Views.py

from django.shortcuts import *
from django.template import RequestContext

from contact.forms import *

def suggestion(request):
    if request.method == "POST":

        form = SuggestionForm(request.POST)

        if(form.is_valid()):
            print(request.POST['title'])
            message = 'success'
        else:
            message = 'fail'

        return render_to_response('contact/suggestion.html',
              {'message': message},
              context_instance=RequestContext(request))
    else:
        return render_to_response('contact/suggestion.html',
                {'form': SuggestionForm()},
                context_instance=RequestContext(request))

Template

 {% extends "base.html" %}

 {% block content %}
 <h1>Leave a Suggestion Here</h2>
 {% if message %}
 {{ message }}
 {% endif %}
 <div>
     <form action="/suggestion/" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit Feedback" />
 </form>
 </div>
 {% endblock %}
1
  • you could use updateview Commented Mar 2, 2014 at 0:06

2 Answers 2

1

Django comes with built in views that handle Creating, updating and deleteing objects using django's forms.

Using UpdateView should allow you to update an object using your ModelForm,

using updateview you have to specify the objects id in the url and tell the view which kwarg to look for the id in. By default I think it uses pk

Sign up to request clarification or add additional context in comments.

1 Comment

This exact answer helped me. @mkriheli got it right also but. this was more specific.
0

The best would be using django's generic class based views:

If you'd still won't to roll your own, you should use the instance keyword, e.g:

suggestion = Suggestion.objects.get(pk=pk)
form = SuggestionForm(instance=suggestion)

You should capture the pk in the url pattern, and make sure your function expects it:

def suggestion(request, pk=None):
    if request.method == "POST":
    ...

Still, the generic class based views do all the hard work for you, so not sure why you would do it manually (learning the mechanism could be one reason).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.