- Django 1.5.4 专题八 Model
- 一.修改article/forms.py的内容如下
- from django import forms
- from models import Article
- class ArticleForm(forms.ModelForm):
- class Meta:
- model=Article
- fields=('title','body','pub_date')
- 二.修改article/urls.py
- from django.conf.urls import patterns,include,url
urlpatterns = patterns('',
- url(r'^all/$','article.views.articles'),
- url(r'^get/(?P<article_id>\d+)/$','article.views.article'),
- url(r'^language/(?P<language>[a-z\-]+)/$','article.views.language'),
- url(r'^create/$','article.views.create'),
- )
- 三.修改article/views.py内容如下
- from django.http import HttpResponse
- from django.shortcuts import render_to_response
- from article.models import Article
- from forms import ArticleForm
- from django.http import HttpResponseRedirect
- from django.core.context_processors import csrf
- def hello(request):
- name ="Mike"
- html =" <html> <body> Hi %s,this seems to have worked! </body> </html> " % name
- return HttpResponse(html)
- def hello_template_simple(request):
- name ="Mike"
- return render_to_response('hello.html',{'name':name})
- def articles(request):
- language ='en-gb'
- session_language ='en-gb'
- if 'lang' in request.COOKIES:
- language = request.COOKIES['lang']
- if 'lang' in request.session:
- session_language =request.session['lang']
- return render_to_response('articles.html',
- {'articles':Article.objects.all(),'language':language,'session_language':session_language})
- def article(request,article_id=1):
- return render_to_response('article.html',
- {'article':Article.objects.get(id=article_id)})
- def language(request,language='en-gb'):
- response =HttpResponse("setting language to %s" % language)
- response.set_cookie('lang',language)
- request.session['lang']=language
- return response
- def create(request):
- if request.POST:
- form=ArticleForm(request.POST)
- if form.is_valid():
- form.save()
- return HttpResponseRedirect('/articles/all')
- else:
- form=ArticleForm()
- args={}
- args.update(csrf(request))
- args['form']=form
- return render_to_response('create_article.html',args)
- 四.修改article/templates/create_article.html的内容如下
- form 可以使用as_p() as_ul() as_table()这些方法
- {% extends "base.html" %}
- {% block sidebar %}
- <ul>
- <li><a href="/articles/all">Cancel</a></li>
- </ul>
- {% endblock %}
- {% block content %}
- <form action="/articles/create/" method="post">{% csrf_token %}
- <ul> {{ form.as_ul}} </ul>
- <input type="submit" name="submit" value="Create Article">
- </form>
- {% endblock %}
- 五.修改article/templates/articles.html的内容如下
- {% extends "base.html" %}
- {% block sidebar %}
- <ul>
- <li><a href="/articles/all">Articles</a></li>
- <li><a href="/articles/create">Create Article</a></li>
- </ul>
- {% endblock %}
- {% block content %}
- <h2>Language is :{{ language }}</h2>
- <h2>Session Language is : {{ session_language }}</h2>
- {% if articles.count > 0 %}
- {% for article in articles %}
- <div>
- <h2><a href="/articles/get/{{ article.id }}/">{{ article.title }}</a></h2>
- <p> {{ article.body|lower|truncatewords:"10" }}</p>
- </div>
- {% endfor %}
- {% else %}
- <p>None to show!</p>
- {% endif %}
- {% endblock %}
- 最终效果如图
作者:u012798391 发表于2013-12-1 2:43:10 原文链接
阅读:95 评论:0 查看评论