Django搭建投票网站--初始化项目结构

  • 网站功能

    • 人们可以查看投票和选举

    • 在管理界面,你可以添加,修改和删除投票

  • 初始化项目目录结构
    在项目文件夹下输入命令:

      django-admin startproject mysite

    项目结构如下:

    mysite/    // 这个目录只是用来存放项目文件的,文件名可以重命名
        manage.py  // 一个命令行实用程序,可以让你与这个Django项目以不同的方式进行交互
        mysite/  // 包目录,(mysite.urls)
            __init__.py  // 空文件,用来指定该目录是一个包
            settings.py  // django项目的设置/配置文件
            urls.py // 网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数
            wsgi.py // WSGI-compatible web服务器入口,打包文件
  • 运行项目

cd mysite
python manage.py runserver

输出:

Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 13, 2017 - 05:58:06
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  • 新建一个app应用

python manage.py startapp learn // learn 是一个app的名称

mysite中会多出一个learn文件夹,目录如下:

learn
    migrations/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
  • 修改mysite/settings.py
    修改INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
 
    'learn',
)

把learn添加到INSTALL_APPS中,主要是让Django能够自动找到learn中的模板文件(learn/templates/…)和静态文件(learn/static/…)

  • 给learn应用定义视图函数
    打开learn/views.py,修改其中源代码,如下:

#coding:utf-8
from django.http import HttpResponse
 
def index(req):
    return HttpResponse(u'This is Django Index')
  • 定义视图函数相关的URL
    打开mysite/mysite/urls.py,修改如下:

urlpatterns = patterns('',
    url(r'^$', 'learn.views.index'),  # new
    # url(r'^blog/', include('blog.urls')),
 
    url(r'^admin/', include(admin.site.urls)),
)
  • 运行
    命令行运行:

python manage.py runserver

输出:

Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 13, 2017 - 05:58:06
Django version 1.11, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

如果是本机访问页面,直接访问:127.0.0.1:8000 就可以了
由于我需要再主机上访问,所以需要运行

python manage.py runserver 0.0.0.0:800
  • 主机访问页面

    • 配置主机hosts
      在hosts文件

    虚拟机ip地址 www.mysite.com
    • 在浏览器上访问 xx.xx.xx.xx:8000
      报错如下:

    Invalid HTTP_HOST header: '192.168.150.128:8000'. You may need to add u'192.168.150.128' to ALLOWED_HOSTS.
      [13/Apr/2017 06:57:55] "GET / HTTP/1.1" 400 61165
    • 修改mysite/mysite/settings.py
      修改ALLOWED_HOSTS

      ALLOWED_HOSTS = [
      '虚拟机ip地址',
      'www.mysite.com'
      ]
     在主机浏览访问:xx.xx.xx.xx:8000或者www.mysite.com:8000 页面输出 This is Django Index
  • 参考
    http://www.ziqiangxuetang.com/django/django-views-urls.html
    https://docs.djangoproject.com/en/1.8/ref/settings/#allowed-hosts

    原文作者:Rhinoceros
    原文地址: https://segmentfault.com/a/1190000009112049
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞