Use Django to quickly develop a blog

Source: Internet
Author: User

 

The Django version used in this system is 1.1, and the database is sqlite3. First, create a project: Django-Admin startproject webblog and enter the webblog directory. Modify settings. py: database_engine = 'sqlite3'
Database_name = 'webblog. db' use the following command to create an app: Python manage. py Startapp blog to enter the blog directory and modify models. py: From Django. DB import models <br/> class reporter (models. model): <br/> full_name = models. charfield (max_length = 70) <br/> def _ Unicode _ (Self): <br/> return self. full_name </P> <p> class article (models. model): <br/> pub_date = models. datetimefield () <br/> headline = models. charfield (max_length = 200) <br/> Article = models. textfield () <br/> reporter = models. foreignkey (Reporter) <br/> def _ Unicode _ (Self): <br/> return self. headlineModify view. py: From webblog. blog. models import * <br/> from Django. shortcuts import render_to_response <br/> from Django. template import context, loader <br/> from Django. HTTP import httpresponse <br/> def index (request): <br/> article_list = article. objects. all () <br/> return render_to_response ('htmls/index.html ', {'Article _ list': article_list })Modify setting. py and add the newly created app and template: template_dirs = (
# Put strings here, like "/home/html/django_templates" or "C:/www/Django/templates ".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"D:/webblog /",
)

Installed_apps = (
'Django. contrib. auth ',
'Django. contrib. contenttypes ',
'Django. contrib. session ',
'Django. contrib. Sites ',
'Webblog. blog ',
'Django. contrib. admin ',
In the weblog directory, run the following command: Python manage. py syncdbcreate an htmlsdirectory under the webblogdirectory, enter the directory, and create index.html: {% extends "blogbase.html" %}

{% Block title %} Articles {% endblock %}

{% Block content %}
<H1> articles

{% For article in article_list %}
<P> headline :{{ article. Headline }}</P>
<P> by {Article. Reporter. full_name }}</P>
<P> published {Article. pub_date | Date: "f j, Y" }}</P>
{% Endfor %}
{% Endblock consumer created blogbase.html: <HTML>
<Head>
<Title >{% block title % }{% endblock %} </title>
</Head>
<Body>
{% Block content %} {% endblock %}
</Body>
</Html> modify the URL. PY, add the access address: Now, the entire Blog system has been configured, we can use Python manage. PY runserver to start the server. Enter http: // localhost: 8000/blog in the address bar to access the blog ~ Of course, there is no information at the beginning. We need to enter some data in the management interface to display it at http: // localhost: 8000/admin/username and password are set for you during syncdb. Add a record after entering it ~~~ For admin management, you need to add Admin. py under the blogFrom Django. contrib import admin <br/> from blog. models import reporter, article <br/> class reporteradmin (Admin. modeladmin): <br/> list_display = ('full _ name',) <br/> search_fields = ('full _ name ',) </P> <p> class articleadmin (Admin. modeladmin): <br/> list_display = ('pub _ date', 'headline', 'Article', 'reporter ') <br/> date_hierarchy = 'pub _ date' <br/> ordering = ('-pub_date',) <br/> raw_id_fields = ('reporter ',) </P> <p> admin. site. register (article, articleadmin) <br/> admin. site. register (reporter, reporteradmin) 

 

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.