Step by step, my first django app
- Django startproject.
$ django-admin startproject mysite
- Django startapp.
$ cd mysite
$ python3 manage.py startapp myapp ### 环境中2.7与3.5版本并存,这里需要指明python3
- Go to pycharm, find settings.py, add app name 'myapp' into INSTALLED_APPS list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', ###
]
- Create a new directory 'templates'(与manage.py同级), and add this directory into settings.py.
If 'APP_DIRS' is True,It tells Django engine that you should look for templates inside installed applications besides the base DIR. If False, then the engine only search mysite/templates.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR), 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
- New a file 'index.html' under 'templates'. Design it.
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="header">
Welcome Django!
</div>
<div class="footer">
<p></p>
</div>
</body>
</html>
- In views.py, define a function. It will return a rendered html
def menublog(request):
return render(request, 'index.html')
- Go to urls.py, define the url to lead all urls beginning with 'index' to 'menublog' defined in vews.py
from myapp.views import menublog
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', menublog)
]
- python manage.py runserver
python3 manage.py migrate
python3 manage.py runserver localhost:9001
- visit 'http://localhost:9001/index/' and 'Welcome Django!' will show on your browser.
Congratulations!
关联templates与静态资源如CSS, img
- settings.py中添加静态资源的路径
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
新建一个与manage.py同级的目录名称为"static"
此目录下存放CSS, img等资源。在templates 中引用静态资源
index.html的第一行添加如下语法加载静态资源,同时将资源的引用路径都替换为src="{% static 'images/0001.jpg' %}"类似的格式。
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The blah</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/homework.css' %}">
</head>
<body>
<div class="header">
![]({% static 'images/blah.png' %})
<ul class="nav">
<li><a href="#" >home</a></li>
<li><a href="#" >site</a></li>
<li><a href="#" >other</a></li>
</ul>
</div>
<div class="main-content">
<h2>The Beach</h2>
<hr>
<ul class="photos">
<li>
![]({% static 'images/0001.jpg' %})
</li>
<li>
![]({% static 'images/0004.jpg' %})
</li>
<li>
![]({% static 'images/0003.jpg' %})
</li>
</ul>
<p>
stretching from Solta to M1jets
</p>
</div>
<div class="footer">
<p>© Mugglecoding</p>
</div>
</body>
</html>
Connect model
这一步实现从数据库中获取数据并填充到templates的指定位置。
- pip install mongoengine
- in setting.py, connect your model
# website是目标数据库的名字
from mongoengine import connect
connect('website', host='127.0.0.1', port=27017)
- go to models.py to define your own model
from django.db import models
from mongoengine import *
# Create your models here.
class menublog(Document):
des = StringField()
title = StringField()
score = StringField()
tags = ListField(StringField())
author = StringField()
# targettable 指定数据库website中的一张数据表的名字
meta = {
'collection': 'targettable'
}
- in views.py, import your model
render() combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.
# 导入models.py中定义的class menublog
from myapp.models import menublog
def myview(request):
data = menublog.objects
# mydata将作为key在templates中被引用
context = {
'mydata': data}
return render(request, 'index.html', context)
- Django template language
引用views.py中myview返回的HttpResponse
......
{% for item in mydata %}
<li>
![]({% static 'images/0001.jpg' %})
<div class="article-info">
<h3><a href="#">{{ item.title }}</a></h3>
<p class="meta-info">
{% for tag in item.tags %}
<span class="meta-cate">{{ tag }}</span>
{% endfor %}
</p>
<p class="description">{{ item.des }}</p>
</div>
<div class="rate">
<span class="rate-score">{{ item.score }}</span>
</div>
</li>
{% endfor %}
......
就像直接使用python一样,for循环也可以嵌套。
至此,对Django的整个框架结构有了初步的印象。
Keep going...