• django基础入门


    django 环境安装

    使用Centos7.4 python2.7.5(默认)

    源码地址

    git clone https://gitee.com/Outsrkem/web.git
    
    yum install zlib-devel bzip2-devel openssl-devel ncurses-devel 
    sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
    yum -y install epel-release
    yum -y install python-pip
    cat << EOF > ~/.pip/pip.conf
    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    [install]
    trusted-host=mirrors.aliyun.com
    EOF
    pip install --upgrade pip
    pip install django
    
    创建模板
    django-admin startproject web  
    创建项目
    python manage.py startapp myweb  
    应用项目(如果是迁移过来的)
    python manage.py migrate
    启动项目
    python manage.py runserver 
    
    web/
    ├── db.sqlite3
    ├── manage.py
    ├── myweb
    │   ├── admin.py
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   ├── tests.py
    │   └── views.py
    └── web
        ├── __init__.py
        ├── settings.py   # 总配置文件
        ├── urls.py
        ├── wsgi.py
    
    

    <font face="Courier New"color='#0A0A0A'> ./web/web/settings.py
    修改监听地址

    ALLOWED_HOSTS = ['10.10.10.35','127.0.0.1']   # 代表监听2个IP
    ALLOWED_HOSTS = ['*']                         # 代表监听本机所有IP
    

    启动

    python manage.py runserver 0:8000             # 0:8000  代表使用配置的ip地址
    

    修改根路由 <font face="Courier New"color='#0A0A0A'> web/web/urls.py

    from django.conf.urls import url,include
    from django.contrib import admin
    urlpatterns = [
          #url(r'^admin/', admin.site.urls),
        url(r'^', include('myweb.urls')),
    ]
    
    

    修改子路由 <font face="Courier New"color='#0A0A0A'>web/myweb/urls.py

    from django.conf.urls import url
    from django.contrib import admin
    from . import views
    urlpatterns = [
        #url(r'^admin/', admin.site.urls),
        url(r'^hello/', views.hello),
    ]
    

    创建视图函数<font face="Courier New"color='#0A0A0A'> web/myweb/views.py

    from django.shortcuts import render
    from django.http import HttpResponse
    # Create your views here.
    def hello(request):
        return HttpResponse('hello word')
    

    页面访问 <font face="Courier New"color='#0A0A0A'> 10.10.10.35:8000/hello


    作者:Outsrkem
    出处:https://www.cnblogs.com/outsrkem/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    java+opencv实现图像灰度化
    java实现高斯平滑
    hdu 3415 单调队列
    POJ 3368 Frequent values 线段树区间合并
    UVA 11795 Mega Man's Mission 状态DP
    UVA 11552 Fewest Flops DP
    UVA 10534 Wavio Sequence DP LIS
    UVA 1424 uvalive 4256 Salesmen 简单DP
    UVA 1099 uvalive 4794 Sharing Chocolate 状态DP
    UVA 1169uvalive 3983 Robotruck 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/outsrkem/p/11787236.html
Copyright © 2020-2023  润新知