$: mkdir root
$: cd root
$: django-admin startproject firstsite
$: cd firstsite
$: atom .
3
,指定用Python3$: python3 manage.py startapp firstapp
'firstapp' ,
$: python3 manage.py migrate
$: python3 manage.py runserver
,打开127.0.0.1:8000即可看到网站成功创建{{ }}
class People(models.Model):
name = models.CharField(null=True, blank=True, max_length=200)
$: python3 manage.py makemigrations
$: python3 manage.py migrate
def first_try(request):
...
t = Template(html_string)
# 模板
c = Context({'person': person})
# 上下文,字典的形式, 与数据相关, Context
web_page = t.render(c)
# 进行渲染,建立某种联系
return HttpResponse(web_page)
# 返回response
# render(request, x.html, context )
# context 上下文,把数据库中的数据与template层展示的数据,在view层,完成映射
def index(request):
context = {}
article_list = Article.objects.all()
context['article_list'] = article_list
index_page = render(request, 'first_web_2.html', context)
return index_page
python3 manage.py createsuperuser
如果想在后台看到model中的数据,需要在admin.py中注册
from firstapp.models import People
admin.site.register(People)
{% %}
{% for item in list %}
{{ item }}
{% endfor %}
{{ }}
{{ article.context | truncatewords:100 }}
'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\', '/')],
{% load staticfiles %}
,css等引入的文件路径也要替换(eg. {% static 'css/semantic.css' %}
),图片引用也需要处理$: python3 manage.py makemigrations
$: python3 manage.py migrate
4, Get一般被用来查阅数据,Post被用来提交数据