使用环境:python3、django2
使用Django创建网页的过程通常分三个阶段:
1-定义URL
2-编写视图
3-编写模板
---------------------------------------------
具体大家可以参看 http://www.cnblogs.com/majianchao/p/8184908.html (Python 项目实践三(Web应用程序))
这也是《Python编程-从入门到实践》这本书所写
可能新手(我也是,呵呵)一上来没弄懂创建网页的流程,会报错,比如:
from django.conf.urls import include,url from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), url(r'', include('learning_logs.urls', namespace='learning_logs')), ]
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'learning_app_logs.urls' from 'D:\\pyworkspace\\learnings\\learning_app_logs\\urls.py'>' does not appear to
have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
其实这都不是什么“错误”,而是你include()指向没东西,所以现在我们需要在文件夹learning_logs中创建另一个urls.py文件。
#定义learning_logs的URL模式
from django.conf.urls import url
form . import views
urlpatterns=[
#主页
url(r'^$',views.index,name='index')
]
写到这里只是第一步:定义URL。还没有编写视图:我们要在learning_logs中的文件views.py中编写:
from django.shortcuts import render
# Create your views here.
def index(request):
'''学习笔记的主页'''
return render(request,'learning_logs/index.html')
现在到了第三步,创建上面render()里面 "learning_logs/index.html"的网页模板
<p>Learning Log</p>
<p>Learning Log helps you keep track of your learning, for any topic you're
learning about.</p>
OK,我这里都是简略写的,只是给出一个解惑,详细的大家可以看书或网页相关文章,我也在学习中,有不对的请包涵。