初学Django--个人博客

it2022-05-05  131

看着视频手把手学Django

做一个个人博客

url->urls.py->views.py->template的编程思路实现代码

写urls.py,写views.py,写template中的html文件

url通过url.py定位到对应的视图函数(views.py中)进行处理,并返回对应的渲染好的模板内容。

在models.py对数据库的表和字段进行定义。

 

urls.py

from django.conf.urls import patterns, include, urlfrom django.contrib import adminurlpatterns = patterns('',    url(r'^admin/', include(admin.site.urls)),    url(r'^$','smallblog.views.home',name='home'),    url(r'^add/$','smallblog.views.add',name='add'),    url(r'^list/$','smallblog.views.list',name='list'),    url(r'^comment/add/','smallblog.views.comment_add'),    url(r'^view/(?P<id>[\d]+)$','smallblog.views.detail',name='detail'),    url(r'^delete/(?P<id>[\d]+)$','smallblog.views.itemDelete',name='delete'),)

关键在正则表达式中,(?P<id>[\d]+)$,$:结束符;[\d]:匹配的内容是数字,<id>并且把该数字赋值给id,供views.py使用该id,确认哪个博客。

 

models.py

from django.db import modelsclass Article(models.Model):    title = models.CharField(max_length=30)    content = models.TextField()

class Comment(models.Model):    Article = models.ForeignKey(Article,related_name='article_comment')    detail = models.TextField()

定义了两个表类Article,Comment,而且Comment的中的Article使用了外键。可以使用python manage.py sqlall 查看具体生成的sql代码。

 

views.py

from django.shortcuts import render,render_to_response,HttpResponseRedirectfrom django.http import HttpResponsefrom smallblog.models import Article,Commentdef home(request):#首页    return render_to_response("home.html")def list(request):#博文列表    articles = Article.objects.order_by("-id").all() #使用了倒序id来排列articles    return render_to_response('list.html',{'articles':articles})#把articles传入到list.html 并渲染返回def add(request):#添加博文    if request.method=='POST':#首先判断request是否POST方法        content = request.POST.get('content',None)#通过get方法,并传入content键,得到从html提交的POST里面content对应的值,返回给变量content        title = request.POST.get('title',None)#通过get方法,并传入title键,得到从html提交的POST里面title对应的值,返回给变量title        new = Article(content=content,title=title)#添加一项记录到表Article中        new.save()#保存给Article到数据库        return HttpResponseRedirect('/list')#重定向链接到/list中    return render_to_response('home.html')#渲染home.htmldef detail(request,id):#博文的详情    article = Article.objects.get(id=id)#通过urls.py的id截取,获得对应博文的id,使用get方法获得对应的Article    comments = Comment.objects.filter(Article=id).order_by("-id").all()#通过Article的id,使用filter过滤Comment表得到对应的comment并通过comment的id进行倒序排序    return render_to_response('detail.html',{'article':article,'comments':comments})#把articles,comments传入到list.html 并渲染返回def itemDelete(request,id):#删除博文    at = Article.objects.get(id=id)    at.delete()#删除id对应的博文    return HttpResponseRedirect('/list')def comment_add(request):#添加评论    if request.method=='POST':#首先判断request是否POST方法        article_id = request.POST.get('article','')#通过get方法,并传入article键,得到从html提交的POST里面article对应的值,返回给变量article_id        detail = request.POST.get('detail','')#通过get方法,并传入detail键,得到从html提交的POST里面detail对应的值,返回给变量detail        if article_id and detail:#判断article_id和detail不为空            comment = Comment()#实例化一个comment对象            comment.Article = Article(id=article_id)#把评论通过外键绑到对应article_id的博文上            comment.detail = detail#把评论内容传入到comment的detail            comment.save()        return HttpResponseRedirect('/view/%s' % article_id)

home.html

 

<form action="/add/" method="post"> #通过post的方法提交表单,并跳转到/add/页面    <p>Title:<input name="title" type="text"></p> #此处name的值,作用就是在视图函数add方法,通过request.POST.get('title',None),获取用户输入的值    <p>Content:<textarea name="content" ></textarea></p>#此处content的值,作用就是在视图函数add方法,通过request.POST.get('content',None),获取用户输入的值    <p><input type="submit"></p></form>

list.py

<a href="/">New</a># 一个跳转链接{% for article in articles %}#for循环    <h1>title:<a href="/view/{{article.id }}">{{ article.title }}</a></h1>#打印从list方法传入article的title内容,并有一个跳转链接,进入详细内容,之前在这里我遇到不知道怎样获取该博文id和怎样把博文id传到urls.py中。原来就这么简单。。。    <h2>content:{{ article.content }}</h2>#打印从list方法传入article的content内容{% endfor %}

detail.html

<a href="/">New</a><h1>title:{{ article.title }}</h1><a href="/delete/{{article.id}}">Delete</a><h2>{{ article.content }}</h2><h1>Comment:</h1>#展示评论{% for comment in comments %}<h1>{{ comment.detail }} </h1>{% endfor %}<form action="/comment/add/" method="post">#提交评论    <p>Commet:<textarea name="detail" ></textarea></p>    <input type="hidden" name="article" value="{{article.id}}">#隐藏了一个input,供视图函数comment_add方法使用,实现评论内容和博文的绑定。    <p><input type="submit"></p></form><a href="/list">Back </a>

 

这是我第一次写博客。可能存在多处问题。欢迎指出。我也是刚刚学做网页。很多东西不会。大家互相学习。

 

转载于:https://www.cnblogs.com/bearlu/p/4992294.html

相关资源:Django-Hexo-Matery:尝试用Django3重新的我的Hexo博客,使用的前端主题是Matery-源码

最新回复(0)