Django Form Question, Last Post Wasn't Formatted Correctly So I Deleted and Made a New One.

31 views
Skip to first unread message

G Z

unread,
May 14, 2014, 1:54:53 AM5/14/14
to django...@googlegroups.com
Below is my code. I am trying to generate a customer signup form from the Customer model in the models.py file.
Below I have created forms.py which is the script that will generate the form data based on what is in the models.py file.
Then in views. I call that forms.py file to run and display the form data.
However no fields are populated just the submit button shows.

 
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>

Venkatraman S

unread,
May 14, 2014, 1:18:58 PM5/14/14
to django...@googlegroups.com
Please pass the form to the template :)


--
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/ec521775-9a04-4989-802a-882f90d65a88%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

G Z

unread,
May 14, 2014, 6:08:38 PM5/14/14
to django...@googlegroups.com
how do i do that?

G Z

unread,
May 14, 2014, 6:22:22 PM5/14/14
to django...@googlegroups.com
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, { 'form': form })   <-- this is how the documentation says to do it is there another way because this gives me an error

Environment:


Request Method: GET

Django Version: 1.6.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'vmware')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/root/djangoprojects/provisioning/vmware/views.py" in index
  16. return render_to_response('index.html', ctx, { 'form': form }) 
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts/__init__.py" in render_to_response
  29.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
  171.         context_instance.pop()

Exception Type: TypeError at /
Exception Value: pop expected at least 1 arguments, got 0



On Wednesday, May 14, 2014 4:08:38 PM UTC-6, G Z wrote:
how do i do that?

Jason Arnst-Goodrich

unread,
May 14, 2014, 7:37:31 PM5/14/14
to django...@googlegroups.com
You're passing two different dictionary parameters there. Try something like this:

        ctx = { 'customers':customers,  'form': form }
        return render_to_response('index.html', ctx)

G Z

unread,
May 15, 2014, 2:36:47 AM5/15/14
to django...@googlegroups.com
thanks
Reply all
Reply to author
Forward
0 new messages