附代碼:
準(zhǔn)備:
在django創(chuàng)建的項目下(具體項目創(chuàng)建看django基礎(chǔ)教程)
1.test.html
2.usersindex.html
3.url路由
4.視圖
5.模型
1.test.html
代碼如下:
<li {%ifequal menu "userIndex" %}class="active"{%endifequal%}><a href="{%url 'userIndex'%}">首頁<span class="sr-only"></span></a></li>
2.usersindex.html
usersindex.html :<p>{{userlist}}</p>
3.url路由from django.conf.urls import include, url
from django.contrib import admin
from Douban.views import *
from Users.views import *
urlpatterns = [
url(r'^admin/$',test),
url(r'^usersindex/$', usersindex, name = 'userIndex'),
]
4.視圖
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.core.urlresolvers import reverse, reverse_lazy
from Users.models import *
from django.template import RequestContext
from django.shortcuts import render_to_response
def test(request):
return render(request,'test.html')
def usersindex(request):
retDict = {}
retDict = {'menu':'userIndex'}# sales
retDict.update(userlist=Users.objects.filter(Usersname = 'Sky').count())
return render_to_response("usersindex.html", retDict,context_instance=RequestContext(request))
5罐旗、模型:
from django.db import models
class Users(models.Model):
Usersname = models.CharField(u'用戶名', max_length=60, unique=True)
Usersemail = models.CharField(u'用戶郵箱', max_length=60)
在使用數(shù)據(jù)庫前要想讀取數(shù)據(jù)要先往數(shù)據(jù)庫中插入數(shù)據(jù),如下
Users.objects.create(Usersname='Sky', Usersemail='11111112qq.com')
可以在python manage.py shell中查看數(shù)據(jù)是否成功插入
更新index的內(nèi)容:點擊test.html中設(shè)置的鏈接公给,通過路由——訪問視圖函數(shù)——返回要更新的網(wǎng)頁index.html