Django-創(chuàng)建我的網(wǎng)頁
源碼地址:https://github.com/edrainann/Django_website
一腋寨、準備環(huán)境
1聪铺、安裝python
2、安裝Django
pip install django
3萄窜、查看Django是否安裝成功
>>> import django
>>> print(django.get_version())
2.2
或者在cmd下檢查
edrainsite>django-admin
Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call sett
ings.configure() before accessing settings.).
參考網(wǎng)址:https://docs.djangoproject.com/en/2.2/intro/install/
二铃剔、開始打造自己的網(wǎng)頁
參考網(wǎng)址:https://docs.djangoproject.com/en/2.2/intro/tutorial01/
1、創(chuàng)建項目
使用 startproject 來創(chuàng)建項目
> django-admin startproject edrainsite
2查刻、運行項目
> python manage.py runserver
這樣就可以運行起來啦
打開網(wǎng)址:http://127.0.0.1:8000/ 進行校驗
3键兜、創(chuàng)建應(yīng)用
> py manage.py startapp hola
4、創(chuàng)建我的第一個視圖
1)hola/views.py 下
from django.http import HttpResponse
def index(request):
return HttpResponse("Hola! This is index~")
def edrain(request):
return HttpResponse("Hola! I'm Edwina")
2)hola/urls.py下(可能需要創(chuàng)建)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('edrain/', views.edrain, name='edrain'),
]
3)edrainsite/urls.py下
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('hola/', include('hola.urls')),
path('admin/', admin.site.urls),
]
此時就可以運行> python manage.py runserver
然后打開網(wǎng)址:http://127.0.0.1:8000/hola/ 或者 http://127.0.0.1:8000/hola/edrain/ 訪問啦