Django doc summary (1)

本文指导读者如何在Windows环境下使用Python和Django框架搭建一个简单的网站,包括安装必要的软件、创建项目、设置视图和URL配置,并运行本地服务器进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Demo

Install Verifing

C:\Users\sam_rui>python --version
Python 3.5.0
C:\Users\sam_rui>python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.9
>>>

Create a demo project

cd into a directory, then create a demo project.

D:\SamProject\django>django-admin startproject samsite

Start a dev server

D:\SamProject\django\samsite>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
December 04, 2015 - 17:25:36
Django version 1.9, using settings 'samsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Start a app

D:\SamProject\django\samsite>python manage.py startapp samapp

Now, the directory tree is like below.

D:\SamProject\django\samsite>tree /F
Folder PATH listing
Volume serial number is 0000003E F203:C3A2
D:.
│  db.sqlite3
│  manage.py
│
├─samapp
│  │  admin.py
│  │  apps.py
│  │  models.py
│  │  tests.py
│  │  views.py
│  │  __init__.py
│  │
│  └─migrations
│          __init__.py
│
└─samsite
    │  settings.py
    │  urls.py
    │  wsgi.py
    │  __init__.py
    │
    └─__pycache__
            settings.cpython-35.pyc
            urls.cpython-35.pyc
            wsgi.cpython-35.pyc
            __init__.cpython-35.pyc
            

Start a new view

add code in file 'samapp/views.py'

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, this is sam site.")
    

add a internal url conf file in samapp directory

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

modify the site url conf

samsite/urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^samapp/', include('samapp.urls')),
]

restart the server and view the url 'http://127.0.0.1:8000/samapp/'

转载于:https://www.cnblogs.com/samrui/p/5037564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值