Re: Adding Forms with ModelForm and Writing Data

7 views
Skip to first unread message

Venkatraman S

unread,
May 16, 2014, 4:29:52 AM5/16/14
to django...@googlegroups.com
  ctx = { 'customers':customers }
to
  ctx = { 'customers':customers , 'form': form }



On Wed, May 14, 2014 at 11:22 AM, G Z <zuk...@gmail.com> wrote:
so I changed my code into 3 parts. If I understood the documentation right this should create a form based on the Customer model in models.py and display it on my index.html

 

forms.py

from django import forms
from .models import Customer

class SignUpForm(forms.ModelForm):
        class Meta:
                model = Customer
models.py

class Customer(models.Model):
    NAME = models.CharField(max_length=200)
    WEBSITE = models.CharField(max_length=200)
    PHONE = models.CharField(max_length=200)
    EMAIL = models.CharField(max_length=200)
    ADDRESS = models.CharField(max_length=200)
    VMIDS = models.CharField(max_length=200)

    def __unicode__(self):
        return self.NAME


views.py

from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms
from .forms import SignUpForm


def index(request):
        form = SignUpForm(request.POST or None)
        if form.is_valid():
                save_it = form.save(commit=False)
                save_it.save()
        customers = Customer.objects.all()
        ctx = { 'customers':customers }
        return render_to_response('index.html', ctx)


index.html

<form action='' method='POST'> {% csrf_token %}
     {{ form.as_p }}
     <p><input type="submit" value="Add"></p>
</form>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a4f836fc-c039-486c-b53b-55ce4178970a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages