我知道这个错误已经提到论坛中的项目数,我尝试了所有但仍然失败..
案例1:从模型导入Category,Product
错误:无法导入 ecomstore.catalog.views。错误是:没有名为模型的模块
案例2:从ecomstore.catalog.models导入Category,Product
错误:无法导入 ecomstore.catalog.views。错误是:没有名为模型的模块
案例3:从catalog.models导入Category,Product
错误:无法导入 ecomstore.catalog.views。错误是:没有名为模型的模块
文件夹结构:目录 models.py views.py urls.py
模型.py
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True,
help_text='Unique value for product page URL, created from name.')
description = models.TextField()
is_active = models.BooleanField(default=True)
meta_keywords = models.CharField("Meta Keywords",max_length=255,
help_text='Comma-delimited set of SEO keywords for meta tag')
meta_description = models.CharField("Meta Description", max_length=255,
help_text='Content for description meta tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'categories'
ordering = ['-created_at']
verbose_name_plural = 'Categories'
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('catalog_category', (), { 'category_slug': self.slug })
class Product(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=255, unique=True,
help_text='Unique value for product page URL, created from name.')
brand = models.CharField(max_length=50)
sku = models.CharField(max_length=50)
price = models.DecimalField(max_digits=9,decimal_places=2)
old_price = models.DecimalField(max_digits=9,decimal_places=2,
blank=True,default=0.00)
image = models.CharField(max_length=50)
is_active = models.BooleanField(default=True)
is_bestseller = models.BooleanField(default=False)
is_featured = models.BooleanField(default=False)
quantity = models.IntegerField()
description = models.TextField()
meta_keywords = models.CharField(max_length=255,
help_text='Comma-delimited set of SEO keywords for meta tag')
meta_description = models.CharField(max_length=255,
help_text='Content for description meta tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
categories = models.ManyToManyField(Category)
class Meta:
db_table = 'products'
ordering = ['-created_at']
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('catalog_product', (), { 'product_slug': self.slug })
def sale_price(self):
if self.old_price > self.price:
return self.price
else:
return None
视图.py
from django.shortcuts import get_object_or_404, render_to_response
from models import Category,Product
from django.template import RequestContext
from django.core import urlresolvers
from django.http import HttpResponseRedirect
from forms import ProductAddToCartForm
from cart import cart
# Create your views here.
print 'prem in catalog views.py'
def index(request,template_name="catalog/index.html"):
page_title = 'Musical instruments and Sheet Music for Musicians'
return render_to_response(template_name,locals(),context_instance=RequestContext(request))
def show_category(request,category_slug,template_name="catalog/category.html"):
c = get_object_or_404(Category,slug=category_slug)
products = c.product_set.all()
page_title = c.name
meta_keywords = c.meta_keywords
meta_description = c.meta_description
return render_to_response(template_name,locals(),context_instance=RequestContext(request))
def show_product(request, product_slug, template_name="catalog/product.html"):
p = get_object_or_404(Product, slug=product_slug)
categories = p.categories.all()
page_title = p.name
meta_keywords = p.meta_keywords
meta_description = p.meta_description
# need to evaluate the HTTP method
if request.method == 'POST':
# add to cart create the bound form
postdata = request.POST.copy()
form = ProductAddToCartForm(request, postdata)
#check if posted data is valid
if form.is_valid():
#add to cart and redirect to cart page
cart.add_to_cart(request)
# if test cookie worked, get rid of it
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
url = urlresolvers.reverse('show_cart')
return HttpResponseRedirect(url)
else:
# its a GET, create the unbound form.Note request as a kwarg
form = ProductAddToCartForm(request=request, label_suffix=':')
# assign the hidden input the product slug
form.fields['product_slug'].widget.attrs['value'] = product_slug
# set the test cookie on our first GET request
request.session.set_test_cookie()
return render_to_response(template_name, locals(),context_instance=RequestContext(request))
ecomstore
catalog
models
urls
views
cart
models
urls
views
cart
设置网址