2.2:当前初步网页达成,但网页上显示的是
You're seeing this message because you have DEBUG = True
in your Django settings file and you haven't configured any URLs. Get to work!
所以说明,当前并没有配置网页。。。。。虽然输入 http://127.0.0.1:8000/admin后,出现了一个跳转,奇怪了。。。
不管怎么说,该添加界面了。
界面嘛,那就是跟 urls相关了, 加入一个view.py,让urls调用。一个html 让view调用
理解的关系为,urls是大页面,view是页面里面的内容,html是view的显示框架。
关联方式为:
在urls中,
from . import view
然后
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^mysite$', view.hello),
]
在 view中,
#from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
context = {}
context['hello_c'] = 'babala'
return render(request, 'hello.html', context)
"""render用法,
render(request, template_name, context=None, content_type=None, status=None, using=None)
Returns a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments.
此方法的作用---结合一个给定的模板和一个给定的上下文字典,并返回一个渲染后的 HttpResponse 对象。
通俗的讲就是把context的内容, 加载进templates中定义的文件, 并通过浏览器渲染呈现. //这段是网上看到的,挺不错的。。
request: 是一个固定参数, 没什么好讲的。
template_name: templates 中定义的文件, 要注意路径名. 比如'templates\polls\index.html', 参数就要写‘polls\index.html’
context: 要传入文件中用于渲染呈现的数据, 默认是字典格式
content_type: 生成的文档要使用的MIME 类型。默认为DEFAULT_CONTENT_TYPE 设置的值。
status: http的响应代码,默认是200.
using: 用于加载模板使用的模板引擎的名称。"""
为了找到 hello.html,需要在 setting.py的TEMPLATES中 添加 'DIRS': [BASE_DIR+"/templates",],
里面放一个 hello.html文件,当然了,为了能继承模板,我们添加的不只这个哦。
还有一个base.html
在Hello.html中,就这样写
{% extends "base.html" %}
{% block mainbody %}<p>{{hello_c}}</p>
{% endblock %}
当然了,这个需要base中,留有block mainbody 让我们能够修改。
好的,那个一个very简单的网页基础,算是写完了。后面的,明天再来。
首先,urls中,
urlpatterns = [
url(r'^mysite$', view.hello), #这个表示mysite网页,也就是http://127.0.0.1:8000/mysite
url(r'^$', view.hello), #这个是主页咯
# url(r'^admin/', admin.site.urls),
]