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 %}