温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

91django_基本使用

发布时间:2020-06-01 11:28:48 来源:网络 阅读:241 作者:chaijowin 栏目:编程语言

 

目录

django. 1

django-admin startproject. 3

django-admin startapp. 5

model... 7

view中使用model... 10

template. 11

view中使用template... 13

去掉urlstatic硬编码:... 15

url反解析:... 15

url命名空间:... 15

form处理:... 16

 

 

 

django

py下一款著名的web框架;

 

框架,是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法;

是可被应用开发者定制的应用骨架;

其它web框架:flasktornado

 

django优:

自带轮子众多,方便快速开发,如ORMAuthCacheTemplateFrom核心组件;

天生的MVC设计模式;

实用且强大的管理后台;

简洁的url设计;

周边插件丰富;

 

django缺:

重;

同步阻塞;

 

django设计哲学:

大而全;

快速开发;

 

django历史:

最初开发者,AdrianSimon

开发背景,World Online维护几个新闻站点,要求快速发布新闻,快速建立其它新闻站点(快速开发、数据驱动);

World Online05年夏天开源;

 

https://www.djangoproject.com/

 

(django) C:\django>pip install django==1.11

(django) C:\django>ipython

In [1]: import django

In [2]: django.get_version()

Out[2]: '1.11'

 

django version

python versions

1.8

2.7,3.2,3.3,3.4,3.5

1.9,1.10

2.7,3.4,3.5

1.11

2.7,3.4,3.5,3.6

2.0

3.4,3.5,3.6

2.1

3.5,3.6,3.7

 

响应流程:

91django_基本使用

 

django-admin startproject

pycharmNew Project-->webprojectC:\webproject

(webproject) C:\webproject>django-admin startproject mysite   #或用python manage.py startproject mysite;此命令脚本在虚拟环境的C:\Users\Administrator\py_env\webproject\Scriptsdjango-admin.exe

91django_基本使用

91django_基本使用

91django_基本使用

mysite/mysite/sessings.py   #项目配置

 

mysite/mysite/urls.py   #入口url

def url(regex, view, kwargs=None, name=None):   #viewfunction object,不是函数调用;view函数的第一参数永远是request,不探讨CBVview函数的返回值永远是HttpResponse对象,显式或隐式;

url(r'^hello/$', hello),   #django默认会给url后加/,此处定义,hello前不带/hello后一定要有/,这样即使用户访问http://127.0.0.1:8000/hello也会自动跳到http://127.0.0.1:8000/hello/上;若此处配置为r'^hello$',用户访问http://127.0.0.1:8000/hello/,访问的地址无论是否带/都会报404;所以记住,此处定义的urlhello前没有/hello后要有/

url(r'^$', views.index, name='index')   #app中的urls.py,可通过name反解出url

url(r'^polls/', include('polls.urls')),   #项目中的urls.pyinclude里是str,在用到时才动态导入;

 

mysite/mysite/wsgi.py   #生产部署时调用

 

注:

include源码,from django.conf.urls import url, include

def include(arg, namespace=None, app_name=None):

 

 

例:

urls.py

from django.conf.urls import url

from django.contrib import admin

 

from django.http import HttpResponse

 

def index(request):

    return HttpResponse('this is index')

 

def hello(request):   #view函数的第一个参数永远是request,不探讨CBV

    return HttpResponse('Hello world')

 

urlpatterns = [   #urlpatterns约定俗成的名字

    url(r'^$', index),

    url(r'^hello/$', hello),   #django默认会给url后加/,此处定义,hello前不带/hello后一定要有/,这样即使用户访问http://127.0.0.1:8000/hello也会自动跳到http://127.0.0.1:8000/hello/上;若此处配置为r'^hello$',用户访问http://127.0.0.1:8000/hello/,访问的地址无论是否带/都会报404;所以记住,此处定义的urlhello前没有/hello后要有/

    url(r'^admin/', admin.site.urls),

]

 

(webproject) C:\webproject\mysite>python manage.py runserver   #可自定义ip:portpython manage.py runserver 0.0.0.0:8080,启动的是django内置webserver,用于测试

 

91django_基本使用

91django_基本使用

 

 

django-admin startapp

 

django appdjango project区别:

app中才能用model

app设计是可插拔的,app不一定在project中;

settings.py中有内置的app,可根据项目情况禁用;

 

 

(webproject) C:\webproject\mysite>django-admin startapp polls   #或用python manage.py startapp polls,新建投票app,在项目根下操作

91django_基本使用

admin.py   #后台管理

apps.py   #1.8ver后加的,app的独立配置

tests.py   #测试用例

views.py   #业务逻辑

migrations/   #model的版本控制有关

 

 

例:

mysite/mysite/settings.py   #在项目settings.py中导入app

INSTALLED_APPS = [

    'polls.apps.PollsConfig',

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

]

 

mysite/polls/views.py

from django.shortcuts import render

from django.http import HttpResponse

 

def index(request):

    return HttpResponse('this is index at polls')

 

mysite/polls/urls.py   #app中的urls.py默认不存在,新建

urlpatterns = [

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

]

 

mysite/mysite/urls.py

urlpatterns = [

    url(r'^polls/', include('polls.urls')),   #include中是str,在用到时才动态导入

    url(r'^admin/', admin.site.urls),

]

 

91django_基本使用

 

 

model

支持sqlitemysqlpostgresqloracle

py3.5以上版本,不支持MySQLdb驱动;

可用pymysqlpy写的;

可用mysqlclientc写的,速度快,forkMySQLdb,官方推荐;

 

ORM,用py的方法、数据结构,来访问db,可兼容不同的DB

 

一个class代表一张表,多对多会产生额外一张关系表;

默认pkid,也可自定义pk

表名默认为$APP_NAME$CLASS_NAME.lower(),表名小写(跨平台支持),可重写;

 

models migrations

定义好models.py需应用到dbdjango为了能跟踪表结构的变化,增加了migration版本控制功能,如果没有该功能,需手动编写表结构变化的语句,重新导入数据;

 

models CRUD

增:

q = Question(**kwargs)   #方式1

q.save()

 

q = Question.objects.create(**kwargs)   #方式2

 

删:

q = Question.objects.get(id=1)

q.delete()

 

Question.objects.filter(id=1).delete()

Question.objects.all().delete()

 

改:

q = Question.objects.get(id=1)

q.question_text = 'some text'

q.save()

 

Question.objects.filter(id=1).update(question_text='why ?')

 

查:

Question.objects.all()

Question.objects.filter(question_text="what's up?")   #objectsmodel默认的manager管理器

Question.objects.get(id=1)

latest_question_list = Question.objects.order_by('-pub_date')[:3]   #默认升序,加上-倒序

 

注:

>>> from django.utils import timezone

>>> import datetime

>>> timezone.now()   #datetime.datetime.now()多了时区,在页面展示时,django内部会转为适合用户所在的时区

datetime.datetime(2019, 1, 2, 7, 2, 18, 244920, tzinfo=<UTC>)

>>> datetime.datetime.now()

datetime.datetime(2019, 1, 2, 15, 2, 32, 837755)

 

models中方法:

def __str__(self):

         return self.question_text

def was_published_recently(self):

         return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

 

 

mysite/mysite/settings.py

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

    }

}

 

 

mysite/polls/models.py

from django.db import models

 

class Question(models.Model):   #一个class代表一张表,多对多会产生额外一张关系表;默认pkid,也可自定义pk;表名默认为$APP_NAME$CLASS_NAME.lower(),可重写

    question_text = models.CharField(max_length=200)

    pub_date = models.DateTimeField('date published')

 

class Choice(models.Model):

    question = models.ForeignKey(Question, on_delete=models.CASCADE)

    choice_text = models.CharField(max_length=200)

    votes = models.IntegerField(default=0)

 

(webproject) C:\webproject\mysite>python manage.py makemigrations   #生成迁移记录

(webproject) C:\webproject\mysite>python manage.py migrate   #应用到db

(webproject) C:\webproject\mysite>sqlite3 db.sqlite3

sqlite> .tables

auth_group                  django_admin_log

auth_group_permissions      django_content_type

auth_permission             django_migrations

auth_user                   django_session

auth_user_groups            polls_choice

auth_user_user_permissions  polls_question

sqlite> .quit

 

(webproject) C:\webproject\mysite>dir polls\migrations\

 驱动器 C 中的卷是 OS

 卷的序列号是 000B-5D26

C:\webproject\mysite\polls\migrations 的目录

2019/01/02  14:24    <DIR>          .

2019/01/02  14:24    <DIR>          ..

2019/01/02  14:24             1,266 0001_initial.py

2019/01/02  11:29                 0 __init__.py

2019/01/02  14:25    <DIR>          __pycache__

               2 个文件          1,266 字节

               3 个目录 77,168,365,568 可用字节

(webproject) C:\webproject\mysite>python manage.py sqlmigrate polls 0001_initial   #查看sql语句是否是指定要求的

 

(webproject) C:\webproject\mysite>python manage.py shell   #进入交互式命令行

>>> from django.utils import timezone

>>> from polls.models import Question,Choice

>>> q = Question(question_text="what's new", pub_date=timezone.now())

>>> q.save()

>>> q.id

1

>>> q.pk

1

>>> q.question_text

"what's new"

>>> q.pub_date

datetime.datetime(2019, 1, 2, 6, 49, 16, 612213, tzinfo=<UTC>)

 

>>> from polls.models import Question,Choice

>>> from django.utils import timezone

>>> q = Question.objects.create(question_text="how are you?", pub_date=timezone.now())

>>> q = Question.objects.create(question_text="what's the weather?", pub_date=timezone.now())

>>> q = Question.objects.create(question_text="fuck you!", pub_date=timezone.now())

>>> q

<Question: fuck you!>

>>> q.was_published_recently()

True

>>> d = timezone.now() - timezone.timedelta(days=2)

>>> q.pub_date = d

>>> q.save()

>>> q.was_published_recently()

False

 

 

view中使用model

from django.http import HttpResponse

from .models import Question

 

def index(request):

    latest_question_list = Question.objects.order_by('-pub_date')[:3]   #默认升序,加上-倒序

    output = ', '.join([q.question_text for q in latest_question_list])

    return HttpResponse(output)

 

def detail(request, question_id):

    return HttpResponse('Your are looking at question {}'.format(question_id))

 

def results(request, question_id):

    response = 'Your are looking at results of question {}'.format(question_id)

    return HttpResponse(response)

 

def vote(request, question_id):

    return HttpResponse('Your are voting on question {}'.format(question_id))

 

 

template

django内置了自己的模板引擎,和jinja很像,使用简单;

 

django默认会在app_name/templates/下寻找模板,在app_name/templates/下再建立app_name,这样app_name/templates/app_name/下存放与该app相关的模板,因为默认django会去所有的app下找模板,可能会优先找到其它app下的模板;

默认会到app_name/static/下寻找静态文件;

 

设置公用templates和公用static

mysite/mysite/settings.py

TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'DIRS': [os.path.join(BASE_DIR, 'templates')],

        'APP_DIRS': True,

……

STATIC_URL = '/static/'

STATICFILES_DIRS = [

    os.path.join(BASE_DIR, 'static'),

]

 

 

>>> from django.template import Template,Context

>>> t = Template('My name is {{ name }}')

>>> c = Context({'name':'jowin'})   #context可以是 dict、属性、方法、tuple|list

>>> t.render(c)

'My name is jowin'

 

>>> t = Template('my name is {{ name }}')

>>> d = {'name':'jowin'}

>>> c = Context(d)

>>> t.render(c)

'my name is jowin'

 

>>> t = Template('my name is {{ user.name }}')   #变量查找,dictattrmethodlisttuple

>>> class Person:

...     def __init__(self,name):

...             self.name = name

...

>>> user = Person('jowin')

>>> user.name

'jowin'

>>> c = Context({'user':user})

>>> t.render(c)

'my name is jowin'

 

>>> t = Template('my name is {{ user.name }}')

>>> class Person:

...     def name(self):

...             return 'jowin'

...

>>> user = Person()

>>> user.name()

'jowin'

>>> c = Context({'user':user})

>>> t.render(c)

'my name is jowin'

 

模板引擎支持循环、判断、过滤器:

for

{% for person in person_list %}

<li> {{ person.name }} </li>

{% endfor %}

 

if

{% if max > 10 %}

<li>max value is {{ max }} </li>

{% else %}

<li>max value is 10 </li>

{% endif %}

 

过滤器:

{{ now | date:"F j,Y" }}

{{ name | length }}

 

 

view中使用template

 

render源码,from django.shortcuts import render

def render(request, template_name, context=None, content_type=None, status=None, using=None):

    """

    Returns a HttpResponse whose content is filled with the result of calling

    django.template.loader.render_to_string() with the passed arguments.

    """

    content = loader.render_to_string(template_name, context, request, using=using)

    return HttpResponse(content, content_type, status)

 

另,render_to_response(),老版本1.6用;

 

 

mysite/polls/views.py

from django.http import HttpResponse

from .models import Question

from django.template import loader

 

# def index(request):

#     latest_question_list = Question.objects.order_by('-pub_date')[:4]

#     template = loader.get_template('polls/index.html')

#     context = {'latest_question_list': latest_question_list}

#     # output = ', '.join([q.question_text for q in latest_question_list])

#     return HttpResponse(template.render(context))   #1

def index(request):

    latest_question_list = Question.objects.order_by('-pub_date')[:4]

    context = {'latest_question_list': latest_question_list}

    return render(request, 'polls/index.html', context)   #2

 

mysite/polls/templates/polls/index.html

<img src="/static/django.png">   #mysite/polls/static/django.png

{% if latest_question_list %}

    <ul>

        {% for question in latest_question_list %}

        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

        {% endfor %}

    </ul>

{% endif %}

 

(webproject) C:\webproject\mysite>python manage.py runserver

91django_基本使用

 

 

去掉urlstatic硬编码:

 

mysite/polls/templates/polls/index.html

{% load static %}

 

<img src="{% static 'django.png' %}">

{% if latest_question_list %}

    <ul>

        {% for question in latest_question_list %}

        <!--<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>-->

        <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

        {% endfor %}

    </ul>

{% endif %}

 

注:

{% url 'detail' question.id %}   #appurls.py中定义的name有关;app namespace改后,'detail'改为'polls:detail'

 

 

url反解析:

 

正解析,url-->view

反解析,view-->url

 

>>> from django.shortcuts import reverse

>>> reverse('detail',kwargs={'question_id':1})

'/polls/1/'

 

另,reverse_lazy(),为解决循环依赖;

 

 

url命名空间:

 

app namespaceinstance namespace

通常使用app namespace

app有多个include,使用instance namespace

 

app namespace

 

mysite/polls/urls.py   #1,建议使用,在app_name/urls.py中定义

app_name = 'polls'

 

mysite/mysite/urls.py

url(r'^polls/', include('polls.urls', app_name='polls')),   #2,在项目下定义mysite/mysite/urls.py

 

 

instance namespace

instance级别,名称不可以重复;

 

mysite/mysite/urls.py

url(r'^polls/', include('polls.urls', namespace='polls')),

 

 

form处理:

 

mysite/polls/templates/polls/detail.html

<h2>{{ question.question_text }}</h2>

 

{% if error_message %}

    <P><strong>{{ error_message }}</strong></P>

{% endif %}

 

<form action="{% url 'polls:vote' question.id %}">

{% csrf_token %}

    {% for choice in question.choice_set.all %}

        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />

        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />

    {% endfor %}

    <input type="submit" value="Vote" />

</form>

 

 

mysite/polls/views.py

from django.shortcuts import render, get_object_or_404, reverse

from django.http import HttpResponse, HttpResponseRedirect

from .models import Question, Choice

from django.template import loader

# from django.core.urlresolvers import reverse

 

 

# def index(request):

#     latest_question_list = Question.objects.order_by('-pub_date')[:4]

#     template = loader.get_template('polls/index.html')

#     context = {'latest_question_list': latest_question_list}

#     # output = ', '.join([q.question_text for q in latest_question_list])

#     return HttpResponse(template.render(context))

def index(request):

    latest_question_list = Question.objects.order_by('-pub_date')[:4]

    context = {'latest_question_list': latest_question_list}

    return render(request, 'polls/index.html', context)

 

def detail(request, question_id):

    # try:

    #     question = Question.objects.get(id=question_id)

    # except Question.DoesNotExist:

    #     return HttpResponse('Not Found', status=404)

    question = get_object_or_404(Question, pk=question_id)

    return render(request, 'polls/detail.html', {'question': question})

 

def results(request, question_id):

    question = get_object_or_404(Question, pk=question_id)

    return render(request, 'polls/results.html', {'question': question})

 

def vote(request, question_id):

    question = get_object_or_404(Question, pk=question_id)

    print(request)

    if request.method == 'POST':

        choice_id = request.POST.get('choice', 0)

        try:

            selected_choice = question.choice_set.get(pk=choice_id)

        except Choice.DoesNotExist:

            return render(request, 'polls/detail.html', {

                'question': question, 'error_message': 'You did not select a choice',

            })

        else:

            selected_choice.votes += 1

            selected_choice.save()

            return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

 

 

mysite/polls/templates/polls/results.html

<h2>{{ question.question_text }}</h2>

 

<ul>

    {% for choice in question.choice_set.all %}

    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>

    {% endfor %}

</ul>

 

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

 

 

 

项目中使用:

pip install pymysql

pip install pillow   #ImageField依赖

 

 

1、  数据库使用mysql

项目目录的__init__.py

import pymysql

pymysql.install_as_MySQLdb()

 

DATABASES = {

  'default': {

      'ENGINE': 'django.db.backends.mysql',

      'NAME': 'mxonline',

      'USER': 'mxonline',

      'PASSWORD': '123456',

      'HOST': '192.168.88.222',

      'PORT': 3306

  }

 

 

2apps目录下统一放app

右键apps-->Mark directory as-->Source Root   #解决编辑器查找import时可在apps中找

 

import sys

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))   #解决命令行环境查找

 

 

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI