Search code examples
pythondjangoformsattributeerrordjango-forms

Django 1.6 Model Forms, AttributeError


Django 1.6. Model Form problem. E-commerce.

Hi there,

I'd really appreciate some help with my seemingly "oh-so-simple (yer-right)" problem. Noob to Django, just trying to get a customer application for a very basic e-commerce website I'm making. The plan is, at this stage, to be able for customers to fill in their name and address details for their delivery. I've read so-so-so many articles online, and on here, with slightly differing approaches, none of which are exactly the (seemingly simple :-)) thing I want. I'm now totally confused...!

My questions are: 1) What does this error mean, and how do I get round it? 2) Is my code right to display a model form? ...and the bonus... 3) Is there a better (read simple) way of making an name and address collector thingy for the site?

I have a model, from which I would think the best plan would be to generate a model form. However, the error I'm now getting (the millionth error of the day) is

AttributeError at /customers/

'list' object has no attribute 'resolve'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/customers/
Django Version:     1.6.5
Exception Type:     AttributeError
Exception Value:    

'list' object has no attribute 'resolve'

Exception Location:     /home/david/.virtualenvs/winestore/local/lib/python2.7/site-packages/django/core/urlresolvers.py in resolve, line 339   

models.py:

from django.db import models
from django.forms import ModelForm
from django.utils.encoding import smart_unicode


    first_name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    email = models.EmailField()
    address_1 = models.CharField(max_length=120)
    address_2 = models.CharField(max_length=120, null=True, blank=True)
    town = models.CharField(max_length=120)

    ~~~~~ Other Fields ~~~~~~

    def __unicode__(self):
        return smart_unicode(self.email)

class CustomerForm(ModelForm):
    class Meta:
        model = Customer
        fields = ['first_name', 'last_name', 'email', ~~~~other fields~~~~~]

urls.py (the main project urls.py has url(r'^customers/', include('customers.urls')), to point it here).

from django.conf.urls import patterns, url
from customers.views import CustomerForm

urlpatterns = patterns('',
    url(r'^$', CustomerForm.as_view, name ='add_customer')),

views.py

from django.http import HttpResponse
from django.views.generic import View
from customers.models import Customer, CustomerForm

class CustomerForm(View):
    class Meta:
        model = Customer
        template_name ='add_customer.html'

        def get_success_url (self):
            return HttpResponse('success.html')

the template add_customer.html

<h1>Add Your details please</h1>
<form action="" method="POST">
    {% csrf_token %}
<ul>
    {{ form.as_ul }}
</ul>
<input id="save_customer" type="submit" value="Save" />

</form>
<a href="{% url "/" %}">Back home</a>

I had a forms.py ,but read it wasn't necessary for a model form (?).

Any help much appreciated.

Edit_V_2: My thanks to Peter and Luis, who respectively noted a trailing comma in the urls.py and that it should be CustomerForm.as_view(), not CustomerForm.as_view.

The page now loads! But is sadly blank. So, so close...!

Edit 3: The server error given is a 405 error. According to this (Django 1.5 giving error 405 for simple form) the problem may be somewhere in the urls.py addresses.


Solution

  • If your urls.py file is exactly as shown, you have a typo in it:

    urlpatterns = patterns('',
        url(r'^$', CustomerForm.as_view, name ='add_customer')),
    

    The trailing comma turns the variable from a patterns object into a 1-length tuple of the patterns object, eg:

    >>> foo = 1,
    >>> type(foo)
    <type 'tuple'>