项目目录结构:
cms APP urls.py:
01 | #!/usr/bin/env python |
02 | # encoding=utf-8 |
03 | from django.conf.urls.defaults import patterns, include, url |
04 | from django.conf import settings |
05 |
06 | urlpatterns = patterns('', |
07 | url(r '^$' , 'cms.views.index' ,name = "index" ), |
08 | url(r '^list/$' , 'cms.views.list' , name = "list" ), |
09 | url(r '^index/$' , 'cms.views.index' , name = "index" ), |
10 | url(r '^add/$' , 'cms.views.add' , name = "add" ), |
11 | url(r '^edit/(?P<id>\d+)/$' , 'cms.views.edit' , name = "edit" ), |
12 | url(r '^search/$' , 'cms.views.search' , name = "search" ), |
13 | url(r '^show_meta/$' , 'cms.views.show_meta' , name = "show_meta" ), |
14 | ) |
cms APP models.py:
01 | from django.db import models |
02 |
03 | class Author(models.Model): |
04 | name = models.CharField(max_length = 60 ) |
05 | |
06 | def __unicode( self ): |
07 | return self .name |
08 |
09 | class Article(models.Model): |
10 | title = models.CharField(max_length = 120 ) |
11 | keywords = models.CharField(max_length = 100 ) |
12 | description = models.CharField(max_length = 300 ) |
13 | content = models.TextField() |
14 | publish_date = models.DateTimeField(auto_now = True ) |
15 | author = models.ForeignKey(Author) |
16 | |
17 | def __unicode__( self ): |
18 | return self .title |
cms APP views.py:
01 | #!/usr/bin/env python |
02 | # encoding=utf-8 |
03 | from django.shortcuts import render_to_response,get_object_or_404 |
04 | from django.http import HttpResponse |
05 | from django.utils import simplejson |
06 | from models import Author,Article |
07 | def index(request): |
08 | return render_to_response( 'cms/index.html' ) |
09 | def list (request): |
10 | articles = Article.objects.order_by( 'id' ) |
11 | return render_to_response( 'cms/list.html' ,{ 'articles' :articles}) |
12 |
13 | def add(request): |
14 | if request.POST: |
15 | title = request.POST.get( 'title' ,'') |
16 | keywords = request.POST.get( 'keywords' ,'') |
17 | description = request.POST.get( 'description' ,'') |
18 | content = request.POST.get( 'content' ,'') |
19 | author = Author.objects.get( id = 1 ) |
20 | article = Article(title = title, keywords = keywords, description = description,content = content,author = author) |
21 | article.save() |
22 | return HttpResponse(simplejson.dumps({ "status" : 1 , "statusCode" : 1 , "navTabId" :request.POST.get( 'navTabId' , None ), "callbackType" :request.POST.get( 'callbackType' , None ), "forwardUrl" :request.POST.get( 'forwardUrl' , None ), "message" :u '添加成功呢' , "data" :u '添加成功呢' }), mimetype = "application/json" ) |
23 | else : |
24 | return render_to_response( 'cms/add.html' ) |
25 |
26 | def edit(request, id ): |
27 | article = get_object_or_404(Article, pk = int ( id )) |
28 | if request.POST: |
29 | article.title = request.POST.get( 'title' ,'') |
30 | article.keywords = request.POST.get( 'keywords' ,'') |
31 | article.description = request.POST.get( 'description' ,'') |
32 | article.content = request.POST.get( 'content' ,'') |
33 | article.author = Author.objects.get( id = 1 ) |
34 | article.save() |
35 | return HttpResponse(simplejson.dumps({ "status" : 1 , "statusCode" : 1 , "navTabId" :request.POST.get( 'navTabId' , None ), "callbackType" :request.POST.get( 'callbackType' , None ), "forwardUrl" :request.POST.get( 'forwardUrl' , None ), "message" :u '编辑成功呢' , "data" :u '编辑成功呢' }), mimetype = "application/json" ) |
36 | return render_to_response( 'cms/edit.html' ,{ 'article' :article}) |
37 |
38 | def delete(request, id ): |
39 | |
40 | if id : |
41 | article = get_object_or_404(Article, pk = int ( id )) |
42 | article.delete() |
43 | return HttpResponse(simplejson.dumps({ "status" : 1 , "statusCode" : 1 , "navTabId" :request.POST.get( 'navTabId' , None ), "callbackType" :request.POST.get( 'callbackType' , None ), "forwardUrl" :request.POST.get( 'forwardUrl' , None ), "message" :u '删除成功呢' , "data" :u '删除成功呢' }), mimetype = "application/json" ) |
44 | def search(request): |
45 | if 'q' in request.GET and request.GET[ 'q' ]: |
46 | q = request.GET[ 'q' ] |
47 | articles = Article.objects. filter (title_icontains = q) |
48 | return render_to_response( 'cms/search.html' , { 'articles' : articles, 'query' : q}) |
49 | else : |
50 | return render_to_response( 'cms/search.html' , { 'error' : True }) |
51 |
52 | def show_meta(request): |
53 | values = request.META.items() |
54 | values.sort() |
55 | return render_to_response( 'cms/show_meta.html' ,{ 'values' :values}) |