使用IntelliJ向导建立Django项目PersonalWebsite,并且建立叫做blog的app
如果在已有项目里建立一个app的话,点击Tools | Run manage.py task,输入startapp
如果不使用IDE的话,用命令行terminal进入希望放置项目的目录下
$ django-admin startproject PersonalWebsite然后进入PersonalWebsite文件夹,并创建名叫blog的app
$ python manage.py startapp blog
$ python manage.py shell
PersonalWebsite/ manage.py PersonalWebsite/ __init__.py settings.py urls.py wsgi.py blog/ views.py models.py ... templates/ blog/ ...
在PersonalWebsite项目目录下创建templates存放各种html文件。
Django的MVC在命名上比较奇怪,Model就是<app_name>/models.py负责定义各种数据模型,Controller是<app_name>/views.py定义如何表达数据模型并且使用哪一个html渲染,View就是templates文件夹了。
当网站主页被访问时,根据PersonalWebsite/settings里的ROOT_URLCONF指定的文件(在这里也就是’PersonalWebsite.urls')进行url和具体的view映射。(具体在PersonalWebiste.url里的具体函数是django.conf.urls.url()。)
blog/views.py
from django.http import HttpResponse def index(request): return HttpResponse("Rango says hey there world!”)
PersonalWebsite/urls.py
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^$', views.home, name='home'), url(r'^blog/', include('blog.urls')), url(r'^admin/', include(admin.site.urls)),
这里的name是对该映射的命名,以便区分,或者反映射。另外要注意import进来的包urls.py应该给每一个app建立,所以映射文件里可以把url映射到文件的相对路径
from django.db import models from django.template.defaultfilters import slugify # Create your models here. class Category(models.Model): name = models.CharField(max_length=128, unique=True) slug = models.SlugField(unique=True) def save(self, *args, **kwargs): self.slug = slugify(self.name) super(Category, self).save(*args, **kwargs) def __unicode__(self): # For Python 2, use __str__ on Python 3 return self.name class Article(models.Model): category = models.ForeignKey(Category) created = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now_add=True) title = models.CharField(max_length=128, unique=True) text = models.TextField() views = models.IntegerField(default=0) slug = models.SlugField(unique=True) def save(self, *args, **kwargs): self.slug = slugify(self.title) super(Article, self).save(*args, **kwargs) def __unicode__(self): # For Python 2, use __str__ on Python 3 return self.title
这里定义了两个数据模型,Category和Article,每个class的属性看名字就能理解了。有一个重点是save:这里运用了slug,有了这个在点击查看博文的时候,url里面会出现博文的名字而不是一些数字或者别的没有意义的符号们。
第一次操作还要创建管理员,现在是管理数据库,后期操作admin。
$ python manage.py createsuperuser
设定好了数据,就要对app注册其变化
$ python manage.py makemigrations APP_NAME $ python manage.py migrate
在shell里检查一下数据库
$ python manage.py shell
# Import the Category model from the Rango application
>>> from rango.models import Category
# Show all the current categories
>>> print Category.objects.all()
[] # Returns an empty list (no categories have been defined!)
# Create a new category object, and save it to the database.
>>> c = Category(name="Test")
>>> c.save()
# Now list all the category objects stored once more.
>>> print Category.objects.all()
[<Category: test>] # We now have a category called 'test' saved in the database!
# Quit the Django shell.
>>> quit()
参考资料:
official Django Tutorial to learn more about interacting with the models
official Django documentation on the list of available commands