[python]1.Django的基本用法
来源:互联网 发布:android 7.0源码 编辑:程序博客网 时间:2024/06/10 21:03
环境准备
- 安装Django:
sudo apt-get install python3-devsudo apt-get install python3-pipsudo pip3 install Django=1.11.4
- 验证安装
python3 -m django --version1.11.4
默认项目
django-admin startproject mysite#django-admin.py startproject mysite 如果上面的命令提示不存在执行这条命令
- 项目目录结构:
mysite/ #项目空间 manage.py #命令行模式 mysite/ #项目名称 __init__.py settings.py #配置项 urls.py #url路由 wsgi.py
- 执行
cd mysitepython3 manage.py runserver 8080
在浏览器中打开http://127.0.0.1:8080
可以看到欢迎信息。
自定义项目
在mysite目录下:
python manage.py startapp pollslsdb.sqlite3 manage.py mysite polls
上述命令类似于在项目下新建一个子module,以便完成不同的任务。
下面开始新建自己页面以及页面逻辑。
编写的顺序为
- 新建页面
- 新建url路由
- 在根url路由中注册
新建页面
gedit polls/views.py
views.py中添加如下代码:
from django.shortcuts import render# Create your views here.from django.http import HttpResponsedef index(request): return HttpResponse('hello world from pools')
添加url路由
gedit polls/urls.py
添加如下代码:
from django.conf.urls import urlfrom polls import viewsurlpatterns=[url(r'^$',views.index,name='index')]
注册路由
gedit mysite/urls.py
urls.py中添加如下代码:
from django.conf.urls import include,urlfrom django.contrib import adminurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^polls/',include('polls.urls')),#add]
好了,以上就完成了自定义的界面和url。
mysite目录下终端执行:
python3 manage.py runserver
打开http://127.0.0.1:8080/polls
可以看到hello world from pools
字样
添加数据库支持
打开polls/models.py
# model定义了项目的数据结构from django.db import modelsclass Question(models.Model): 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)
打开mysite/settings.py修改:
# Application definitionINSTALLED_APPS = [ 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',]
终端执行:
python3 manage.py makemigrationspython3 manage.py migrate
准备数据
python3 manage.py shellIn [1]: from polls.models import Question, ChoiceIn [2]: Question.objects.all()Out[2]: <QuerySet []>In [3]: from django.utils import timezoneIn [4]: q=Question(question_text='what\'s new?',pub_data=timezone.now())In [5]: q.save()In [6]: q.idOut[6]: 1In [7]: q.question_textOut[7]: "what's new?"In [8]: q.pub_dateOut[8]: datetime.datetime(2017, 8, 19, 10, 13, 27, 102521, tzinfo=<UTC>)In [9]: Question.objects.all()Out[9]: <QuerySet [<Question: Question object>]># Create three choices.In [10]: q.choice_set.create(choice_text='Not much', votes=0)Out[10]:<Choice: Not much> In [11]: q.choice_set.create(choice_text='The sky', votes=0)Out[11]:<Choice: The sky>In [12]: q.choice_set.create(choice_text='Just hacking again', votes=0)Out[12]:<Choice: Just hacking again>
admin
python3 manage.py createsuperuser #创建admin账户python3 manage.py runserver
启动网站,在浏览器输入http://127.0.0.1:8080/admin
可以进入admin登陆界面,输入admin账户帐号和密码即可以进行登录。
可以看到并没有Question的管理选项。
打开polls/admin.py
添加:
from django.contrib import adminfrom .models import Questionadmin.site.register(Question)
刷新之后可以看到:
即可以对Question进行管理。
以上就完成了基本的Django的学习。
阅读全文
0 0
- [python]1.Django的基本用法
- Django 基本命令用法
- [python]Graphlab的基本用法
- django中urls和views的基本用法
- python django的模板的基本使用方式
- Python Django项目基本配置
- django学习教程(一)如何安装python django和基本的操作
- python 之 lambda 表达式的基本用法
- Python包和类的基本用法
- Python正则式的基本用法
- python str.format()方法的基本用法
- Python 自动化测试 的 基本用法实例
- python列表的基本用法介绍
- python爬虫入门-urllib的基本用法
- Python正则式的基本用法
- python 中一些基本函数的用法
- Python包和类的基本用法
- 【Python】urllib的基本用法01
- 算法入门经典——果园里的树
- 23.odoo入门——工作杂记
- [12] LNMP架构
- A*算法浅析
- 并查集-总结
- [python]1.Django的基本用法
- HDU 6153-A Secret(kmp&&ccpc)
- ReentrantLock的原理和应用
- linux label
- Linux Centos6.8 安装配置Readis3.2.8
- 用transient修饰的成员变量不能序列化,为什么ArrayList集合可以实现序列化
- 如何更好地限制一个UITextField的输入长度
- iOS OpenGL ES入门详解
- 团队配合