django做一个最简单的店商网站

书面作业

    实现订单列表
    订单列表的分页处理
    实现用户登录后只能看到跟登录用户有关的订单列表
    以上实现的结果界面切图,上传到作业中

 

代码片段如下:
models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Product(models.Model):
    name = models.CharField('produdct name',max_length=30)
    price = models.FloatField('price',default=10)
    ptype = models.ForeignKey('Ptype')
    img = models.ImageField('img',upload_to="product",max_length=100)
    def __unicode__(self):
        return "%s -->%f "% (self.name,self.price)
class Ptype(models.Model):
    name = models.CharField('type',max_length=10)
    def __unicode__(self):
        return "%s"% (self.name)
class Order(models.Model):
    odate = models.DateTimeField("order date",auto_now=True)
    product = models.ForeignKey("Product")
    user = models.ForeignKey(User)
    def __unicode__(self):
        return "%s-->%s-->%s" % (self.product.name,self.user,self.odate)
class UserProfile(models.Model):
    SEX_CHOICES =(('1','male'),
                  ('2','female'),
                  )
    user =models.OneToOneField(User)
    nickname = models.CharField("nickname",max_length=30)
    sex = models.CharField("sex",max_length=1,choices=SEX_CHOICES,default='1')
    address = models.CharField("address",max_length=100,null=True)

 

admin.py

from django.contrib import admin
from models import Product,Ptype,Order, UserProfile

admin.site.register(Product)
admin.site.register(Ptype)
admin.site.register(Order)
admin.site.register(UserProfile)

 

views.py

# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from models import Product,Ptype,Order,UserProfile
from django.core.paginator import Paginator
def index(request):
    pagenum = 1
    if request.REQUEST.has_key("pid"):
        pagenum = request.REQUEST['pid']
    products = Product.objects.all()
    p = Paginator(products,2)
    page = p.page(pagenum)
    return render_to_response("index.html",{"user":request.user,"p":p,"page":page},
                              context_instance = RequestContext(request)                              )

def query(request):
    pagenum = 1
    if request.REQUEST.has_key("pid"):
        pagenum = request.REQUEST['pid']
    products=[]
    #products=Product.objects.filter(ptype__id =1)
    if request.POST.has_key("productname"):
        products=Product.objects.filter(name__contains=request.REQUEST["productname"])
    else:
        products = Product.objects.all()
    p = Paginator(products,2)
    page = p.page(pagenum)  
    return render_to_response("index.html",{"user":request.user,"p":p,"page":page},
                              context_instance = RequestContext(request))

def order(request):
    pagenum = 1
    if request.REQUEST.has_key("pid"):
        pagenum=request.REQUEST['pid']  
    orders = Order.objects.filter(user = request.user)
    p = Paginator(orders,2)
    page = p.page(pagenum) 
    return render_to_response('order.html', {"user":request.user,"p":p,"page":page },
                              context_instance=RequestContext(request))

 

urls.py

# -*- coding=utf-8 -*-
from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
url(r'^$', 'order.views.index'),
url(r'^index/$', 'order.views.index'),
url(r'^query$', 'order.views.query'),
url(r'^order$', 'order.views.order'),
#url(r'^search/$', 'order.views.search'),
# url(r'^shop/', include('shop.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

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

)

image

 

image

 

image

--------------------------------------------------------------------------------------------------------------------

遇到的问题:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb8 in position 115: invalid start byte
把模板文件 转成UTF-8的

后台看不到样式
settings.py 改一下就

STATIC_ROOT = '/static/'
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

您可以选择一种方式赞助本站