开发环境:
Eclipse 版本 luna
PyDev 版本 PyDev 2.8.2
Django 版本 Django-1.6.2
1、新建一个Django项目,采用默认的SQLite数据库,然后新建一个Application,在settings.py文件中可以看到
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
2、修改models.py、views.py文件
from django.db import models
from django.contrib import admin
class Author(models.Model):
name = models.CharField(max_length = 80)
age = models.IntegerField(blank = True, null = True)
sex = models.CharField(max_length = 10)
email = models.EmailField()
def __unicode__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length = 200)
content = models.TextField()
attach = models.FileField(upload_to = "datasite/blog/file/")
pub_time = models.DateTimeField('PUB_TIME')
author = models.ForeignKey(Author)
def __unicode__(self):
return self.title
class ArticleComment(models.Model):
content = models.TextField()
pub_time = models.DateTimeField('PUB_TIME')
author = models.ForeignKey(Author)
article = models.ForeignKey(Article)
def __unicode__(self):
return self.content
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'content', 'attach', 'pub_time')
class AuthorAdmin(admin.ModelAdmin):
list_display = ('name', 'age', 'sex', 'email')
admin.site.register(Article, ArticleAdmin)
admin.site.register(Author, AuthorAdmin)
from django.template import loader, Context, RequestContext
from django.http import HttpResponse, HttpResponseRedirect
from blog.models import Author, Article
from django.shortcuts import render_to_response
def authorList(request):
author_list = Author.objects.all()
return render_to_response('author_list.html',
{'author_list' : author_list}, context_instance=RequestContext(request))
def articleList(request):
article_list = Article.objects.all()
return render_to_response('article_list.html',
{'article_list' : article_list}, context_instance=RequestContext(request))
def article(request, aid):
print 'article_id %s' %aid
article = Article.objects.get(id = aid)
return render_to_response('article.html',
{'article' : article}, context_instance=RequestContext(request))
def articleUpdate(request, aid):
article = Article.objects.get(id = aid)
if request.POST.has_key('title'):
article.title = request.POST['title']
if request.POST.has_key('content'):
article.content = request.POST['content']
article.save()
return HttpResponseRedirect('/blog/')
def articleDelete(request, aid):
print 'article_id %s' %aid
article = Article.objects.get(id = aid)
article.delete()
return HttpResponseRedirect('/blog/')
from blog.views import *
admin.autodiscover()
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/$', articleList),
url(r'^blog/(?P\d+)/$', article),
url(r'^blog/(?P\d+)/u/$', articleUpdate),
url(r'^blog/(?P\d+)/d/$', articleDelete),
]
base.html
datasite.com
{% block content %}
{% endblock %}
article_list.html
{% extends "base.html" %}
{% block content %}
Articles
Title
Content
Pub_Time
Author
Operation
{% for article in article_list %}
{{article.title}}
{{article.content}}
{{article.pub_time}}
{{article.author.name}}
Detail
Delete
{% endfor %}
{% endblock %}
article.html
{% extends "base.html" %}
{% block content %}
Article
Title : {{article.title}}
Content : {{article.content}}
PubTime : {{article.pub_time}}
Attach : {{article.attach}}
Author : {{article.author.name}}
{% endblock %}
可以在http://127.0.0.1:8000/admin/blog/article/下做新增操作
可以在http://127.0.0.1:8000/blog/下做查看、修改、删除操作
注意事项:
第一种解决方法在表单里加上{% csrf_token %}就行了。
第二种解决方法是在Settings里的MIDDLEWARE_CLASSES增加配置:
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
第三种方法在view里面强制使用RequestContext 代替Context表单action字段没有以 '/' 结尾
讲action 修改为 action="/a/b/"即可修复
在view 中通过HttpResponseRedirect 实现 redirect