码迷,mamicode.com
首页 > 其他好文 > 详细

Django---模板使用

时间:2018-01-28 11:21:54      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:min   mode   简洁   取出   target   取数据   for   templates   att   

一、上节我们创建了Django -- 开始创建应用,这节我们接着上节的内容创建Django模板的使用,urls分路由的设置。

1.首先想想,我们通过什么取到每一篇文章呢?

我们创建Djangoapp之后我们发现多了一个id 字段,这是主键,唯一的标识。

技术分享图片

2.在 我们创建的articile目录views下首先写入一个方法:

使用Artcirle.objects.get() 方法获取数据
from django.shortcuts import response
from django.http import HttpResponse,Http404
from .models import Artcirle #导入方法

def Arcite_deaile(request,article_id):
    try:
        article = Artcirle.objects.get(pk = article_id)#关联数据替换
            article = raise Http404(not_exit)
        

在url里面设置:

这里的 <int:article_id> 与前面的pk主键关联。
from django.contrib import admin
from django.urls import include,path
from .import views
urlpatterns = [
    path(admin/, admin.site.urls),
    path(‘‘,views.index),#参数,方法
    path(article/<int:article_id>,views.Arcite_deaile,name=Arcite_deaile),
]

3.我们可能会访问到不存在的界面,这里就需要使用Http404,方法,返回一个错误提示:

方法一:

try:
    article = Artcirle.objects.get(pk = article_id)
except Artcirle.DoesNotExist:
    raise Http404(没有!)

方法二:

还有一个get方法:get_object_or_404 这种方法代码更加简洁

article = get_object_or_404(Artcirle,pk = article_id)

4.让数据显示在模板上

需要先在app应用下建立一个templates文件夹,建立html模板

方法一:模板我们主要使用render方法传数据给前端render(模板名( request,设立的数据库class Artcirle(models.Model)名,还需建立一个字典名contenx)

方法二:是简化版使用render_to_response只需后两个变量就可以了

from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponse
from .models import Artcirle #导入方法
# Create your views here.
def Arcite_deaile(request,article_id):
    # try:
    article = get_object_or_404(Artcirle,pk = article_id)#模型+条件 url (article_id)和pk对应
    # article = Artcirle.objects.get(pk = article_id)#关联数据替换
    # article = raise Http404(not_exit)
    contenx = {}
    contenx[article_obj] = article
    return render_to_response(deail.html,contenx)
    # # except Artcirle.DoesNotExist:
    # #     raise Http404(‘没有!‘)

在html中使用{{}}来获得数据

<html>
<header>
</header>
<body>
    <h2>{{article_obj.title}}</h2>
    <hr>
    <p>{{article_obj.content}}</p>
</body>
</html>
<!-- "<h2>标题%s</h2></br>内容%s"%(article.title,article.content) -->

5.获取列表项

使用Artcirle.objects.all()方法获取所有的数据之后再在templates之中设立html.list

# #获取文章列表
def Arcite_list(request):
    articles = Artcirle.objects.all()#获取所有的数据
    #创建字典传值
    contenx = {}
    contenx[article_list] = articles #这里的article_list是传给模板显示的值
    return render_to_response(list.html,contenx)

同样的设置一个方法之后设置路由:

path(articile/,Arcite_list,name = Arcite_list),

6.最后实现一个点击列表项跳转到详情的效果:

views代码:

from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponse
from .models import Artcirle #导入方法
# Create your views here.
def Arcite_deaile(request,article_id):
    # try:
    article = get_object_or_404(Artcirle,pk = article_id)#模型+条件 url (article_id)和pk对应
    # article = Artcirle.objects.get(pk = article_id)#关联数据替换
    # article = raise Http404(not_exit)
    contenx = {}
    contenx[article_obj] = article
    return render_to_response(deail.html,contenx)
    # # except Artcirle.DoesNotExist:
    # #     raise Http404(‘没有!‘)
    
# #获取文章列表
def Arcite_list(request):
    articles = Artcirle.objects.all()#获取所有的数据
    #创建字典传值
    contenx = {}
    contenx[article_list] = articles #这里的article_list是传给模板显示的值
    return render_to_response(list.html,contenx)

html部分代码:(deail.html)详情页

<html>
<header>
</header>
<body>
    <h2>{{article_obj.title}}</h2>
    <hr>
    <p>{{article_obj.content}}</p>
</body>
</html>
<!-- "<h2>标题%s</h2></br>内容%s"%(article.title,article.content) -->

(list.html)列表页

<html>
<header>
</header>
<body>
    {%comment%}<!-- {% for article in article_list%}以循环的方式将列表读取出来 --> 
    <!-- {{article_list}} 以字典里的东西返回 --> 
    <!--  -->
    {%endcomment%}
    {% for article in article_list %}
    {%comment%}##写法一:<a href = "/article/{{article.pk}}">{{article.title}}</a>
    这里的article.pk是当前页({% for article in article_list %})循环的那个article 
    {%endcomment%}
    {#写法二:使用url规定的别名#}
    <a href = "{%url ‘Arcite_deaile‘ article.pk %}">{{article.title}}</a>

    {% endfor %}
</body>
</html>

 

 

Django---模板使用

标签:min   mode   简洁   取出   target   取数据   for   templates   att   

原文地址:https://www.cnblogs.com/jeepzp/p/8370568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!