Django Development Easy Blog (ii)

Source: Internet
Author: User

In this section we will look at the use of templates and views, URLs.

I. Using a template

Create a new Templates folder in the blog directory and create a new base.html file under the Templates folder. The directory structure is as follows

templates/    base.html
View Code

Writing base.html files

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>{% block title%} {% Endblock%}</title></Head><Body>    <H1>A simple blog</H1>{% block content%} {% Endblock%} {% block footer%}<P>Thanks for visiting my site!</P>{% Endblock%}</Body></HTML>
View Code

Base.html defines a simple HTML framework that will then be used on all pages.

Create a new blog_list.html under the Templates directory

{% extends "base.html"%}{% block title%} Blog list {% endblock%}{% block content%}<Divclass= "Content">{% for blog in blogs%}<H3>{{Blog.caption}}</H3>        <Div>{{Blog.content}}</Div>{% endfor%}</Div>{% Endblock%}
View CodeTwo. Configure the View file

Edit views.py in the blog directory

 from Import Render_to_response  from Import Blog def blog_list (Request):     = Blog.objects.all ()    return render_to_response ("blog_list.html ", {"blogs": Blogs})
View CodeThree. Configure URLs

Edit urls.py in the Web directory

 urlpatterns = Patterns ( " ,  #   Examples:  #   URL (r ' ^$ ', ' web.views.home ', name= ' home '),  #   URL (r ' ^blog/', include (' Blog.urls ')),   URL (r   '  
View Code

Add a urls.py file to the blog directory

 from Import *= patterns ('blog.views'),    URL (r' ^bloglist/$ ' ' blog_list ', name='bloglist'),)
View Code

Use admin to add a few tags, blog, and author, finally run the server, open 127.0.0.1:8000/web/bloglist, display the following interface

Iv. Add a blog display page

Add blog_show.html in the Templates directory.

{% extends "base.html"%}{% block title%} {{blog.caption}} {% Endblock%}{% block content%}<Divclass= "Content">    <H2>Blog Show</H2>    <h4>{{Blog.caption}}</h4>    <Div>{{Blog.content}}</Div></Div>{% Endblock%}
View Code

Adding Blog_show View functions in the views.py file

From django.http import http404def blog_show (Request, id= "):    try:        blog = Blog.objects.get (id=id)    except Blog.doesnotexist:        raise Http404    return render_to_response ("blog_show.html", {"blog": Blog})
View Code

Modify the blog directory below the urls.py, add the following content

URL (r'^blog/(? p<id>\d+)/$','blog_show', name='  Detailblog'),

Modify Blog_list.html

< H3 > {{Blog.caption}} </ H3 >

Change it to

< H3 >    <  href= "{% url ' detailblog ' blog.id%}">        {{blog.caption}}     </ a > </ H3 >

The special note is that Detailblog must bring the single quotation mark, again refreshes the next blog_list page, the blog title handy becomes the link.

  

 

  

  

  

Django Development Easy Blog (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.