Last active
August 29, 2015 13:59
-
-
Save shashisp/10983095 to your computer and use it in GitHub Desktop.
django forms example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "base.html" %} | |
{% block content %} | |
<hr/> | |
<center> | |
<form action="/adddata/" class="form-vertical" method="post"> {% csrf_token %} | |
<p> | |
{{ form.as_p }} | |
</p> | |
<input type="submit" class="btn btn-info" name="submit" value="Register"> | |
</form> | |
</center> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from models import SimpleModel | |
class SimpleModelform(forms.ModelForm): | |
class Meta: | |
model = SimpleModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class SimpleModle(models.Model): | |
title = models.CharField(max_length=20) | |
body = models.TextField() | |
def __unicode__(self): | |
return self.title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import patterns, include, url | |
urlpatterns = patterns('', | |
url(r'^adddata/$', 'yourapp.views.adddata'), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def adddata(request): | |
if request.POST: | |
form = SimpleModelForn(request.POST) | |
if form.is_valid(): | |
form.save() | |
return HttpResponseRedirect('/') | |
else: | |
form = OrganisationForm() | |
args = {} | |
args.update(csrf(request)) | |
args['form'] = form | |
return render_to_response('adddata.html', args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment