1
#### model using ModelForm: models.py
from django.db import models
from django.forms import ModelForm

class customers(models.Model):
    name = models.CharField(max_length=50)
    custAdd = models.TextField()

    class Meta:
            db_table = 'tb_amit_test'
            ordering = ['-name']
            verbose_name_plural = 'customers'

    def __unicode__(self):
            return self.name

    @models.permalink
    def get_absolute_url(self):
            return ('customers_customers', (), { 'customers_name': self.name })

class customerForm(ModelForm):
    class Meta:
            model=customers


#### View:views.py

from django.shortcuts import render_to_response
from mtcc_customer_db import customers
from mtcc_customer_db import customerForm
from django.template import RequestContext


def adddata(request):
    if request.method == 'POST':
            f=custform(request.POST)
            if f.is_valid():
                    newcust=f.save(commit=False)
                    newcust.save()
            return HttpResponseRedirect('/')
    return render_to_response('index.html',
context_instance=RequestContext(request))

#### URLs:
from django.conf.urls import patterns, include, url
from mtcc_customer_db import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
 url(r'^admin/', include(admin.site.urls)),)

urlpatterns +=patterns('mtcc_customer_db.customers.views',
(r'^customers/$', 'adddata'),)

### Template: customer.html

{% extends "base.html" %}

{% block site_wrapper %}
    <div id="main">
    {% include "tags/navigation.html" %}

    <a href="#content" class="skip_link">Skip to main content</a>
    <form action="." method="post">
    <input type="text" name="name" id="name" value="{{name}}">
     <input type="text" name="custAdd" id="custAdd" value="{{custAdd}}">
     <input type="submit" value="Submit">
    </form>.........
{% endblock %}

我在浏览器中收到错误:

请求方法:GET 请求 URL:someaddress.customers/ Django 版本:1.4.3 异常类型:ImportError 异常值:

--->>无法导入名称customerForm

我哪里错了??请帮忙

4

2 回答 2

0

试试这个:

from your_module_name.models import customerForm

在您的 views.py 文件中

于 2013-01-31T10:44:39.527 回答
0

表格在 models.py (*) 中,所以你应该做from mtcc_customer_db.models import customerForm.

(*) 请注意,无论如何它可能应该在 forms.py 中,但仍然如此。

于 2013-01-31T10:46:48.960 回答