blog.ericsk.org/archives/815
There are no killers in the title, you'll know it by the book.
On the official website of the Ruby on Rails, there are 15 minutes to make a weblog screencast, which is a quick demo of RoR . But never too superstitious. Only RoR can be so fast, and by the recent squeeze of a little time to look at Django, I would also like to start as a "10 to make a blog challenge. (since it is not yet used to make screen cast software, you can only introduce it in words.) This article is just a description of the build process, so don't introduce the Django environment's installation setting, and please forgive the reader.
A little bit of Django, it's a WEB development Framework (framework) based on Python language. build project and App
In a Django environment, a project can have a lot of application, so that the application of the same project can share the same set of settings. So we first execute django-admin.py under the command line to build a project:
Let's change the setting of the demo/settings.py, where we'll use the sqlite3 as the engine for the library, and then we'll produce a demo.db file as a repository, and then set up some time zones and template records:
[Code lang= "Python"]
Database_engine = ' Sqlite3 '
database_name = ' demo.db '
...
Time_zone= ' Asia/taipei '
Language_code= ' ZH-TW '
...
Template_dirs = (
...
'.',
)
Installed_apps = (
' Django.contrib.admin ',
..
' Demo.blog ', # will be produced in a moment.
)
[/code]
And then set up a blog app in demo catalogue:
Then create the required model (DB Table) in demo/blog/models.py:
[Code lang= "Python"]
From django.db import Models
Class Category (models. Model):
Name = models. Charfield (MAX_LENGTH=32)
def __unicode__ (self):
Return Self.name
Class Admin:
Pass
Class Article (models. Model):
title = models. Charfield (max_length=64)
Published_at = models. Datetimefield (' date published ')
Content = models. TextField ()
Category = Models. ForeignKey (Category)
def __unicode__ (self):
Return Self.title
Class Admin:
Pass
[/code]
Then you can look at the Django generated SQL DDL, and make sure you use the SYNCDB to build the Data library table:
It will require you to create a manager account, which is the account used to manage the article in the interface. After you have done this, modify the contents of the demo/urls.py:
[Code lang= "Python"]
From django.conf.urls.defaults Import *
Urlpatterns = Patterns ("",
(R ' ^blog/', include (' Demo.blog.urls ')),
(R ' ^admin/', include (' Django.contrib.admin.urls ')),
)
[/code]
First create a urls.py file under demo/blog/, then enter Python manage.py runserver under the command line, and then open the browser to enter the Web site http://localhost:8000/admin/ There's a back-facing interface that you can use.
The interface of the article is still very useful
You can directly build the content of Category and artical after you login, isn't that a form of an article now? It's not going to be over 5 minutes until the start of the operation. article List, single article
After you can make an article, of course you can browse it, so we'll open demo/blog/urls.py this file and paste the following code:
[Code lang= "Python"]
From django.conf.urls.defaults Import *
From Demo.blog.models import Article
Info_dict = {
' Queryset ': Article.objects.all (),
}
Urlpatterns = Patterns ("",
(R ' ^$ ', ' django.views.generic.list_detail.object_list ', info_dict),
(R ' ^ (?) pd+)/$ ', ' Django.views.generic.list_detail.object_detail ', info_dict),
)
[/code]
And then create demo/blog/article_list.html and demo/blog/article_detail.html These two files, to separate the list of articles and the contents of the article:
Article_list.html:
[Code lang= "Python"]
{% if object_list%}
{% for article in object_list%} {{Article.title}}}
{% ENDFOR%}
{% Else%}
Sorry, no an article.
{% ENDIF%}
[/code]
Article_detail.html:
[Code lang= "Python"] Title: {{Object.title}} {{Object.published_at}} {{object.content}}}
Modify
Back
[/code]
This will soon make a very similar blog (just can not leave a message XD), the Web site in http://localhost:8000/blog/.
It's going to be quick. Of course, I ignored the art, and no one made the back, but the Web is getting worse .... I'm afraid the more that this development framework is, the more the bosses feel that they have nothing to do with the web.
It's worth mentioning that Django, after Runserver, will compile the code into a PYc file, and it should look more efficient, maybe wait until I have a little more fun to do some experiments.