当前位置:网站首页>Django 2 ----- 数据库与Admin
Django 2 ----- 数据库与Admin
2022-07-25 12:52:00 【WFForstar】
一. 视图函数
(1). 基本概念
视图的作用是接受和发送Web请求,主要用来处理网站的业务逻辑
(2). 开发第一个视图
在polls/views.py下,写入以下代码
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("欢迎来到投票系统!")
注:request这个参数是必须要传的,具体是什么后面会介绍
写完视图函数后,先在poll目录下配置分布式路由,新建一个urls.py文件,写入以下代码
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
再去主目录的urls.py将这个路由包括进去
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('index', include('poll.urls')),
]
在浏览器中打开”http://127.0.0.1:8000/index“,如果没报错,应该就看到页面了
二. 对数据库的配置
这里用MySQL数据库
直接进入到settings.py,大概76行的位置
要先在mysql中创建相对应的数据库哦
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test1_0719', # 名字可以随便取
'USER':'root',
'PASSWORD':'123456',
'HOST':'127.0.0.1', # 本机环回地址
'PORT':'3306', # 默认端口号3306
}
}
三. 建立模型
在poll/models.py写入以下代码:
import datetime
from django.db import models
from django.utils import timezone
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField(verbose_name='date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0) # 整数类型,映射到数据库中会变成11位的int类型
def __str__(self):
return self.choice_text
接着在命令行执行以下命令:
python3 manage.py makemigrations
python3 manage.py migrate
这两个脚本命令是为了告诉MySQL数据库,models发生更改。
四. Admin模块
python manage.py createsuperuser
接下来跟着命令行做就行了
在浏览器输入http://127.0.0.1:8000/admin/,就可以进入superuser界面

在poll/admin.py输入以下代码
from django.contrib import admin
# Register your models here.
from . models import Question
admin.site.register(Question)
刷新页面就可以得到新的界面,然后点击加号就可以编辑属于自己的问题。
现在我们有个后端页面,但是没有前端页面,这在Django—3中会讲到。
边栏推荐
- conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题
- Atcoder beginer contest 261e / / bitwise thinking + DP
- OAuth, JWT, oidc, you mess me up
- State mode
- 零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
- R语言GLM广义线性模型:逻辑回归、泊松回归拟合小鼠临床试验数据(剂量和反应)示例和自测题
- massCode 一款优秀的开源代码片段管理器
- 工业互联网的内涵及其应用
- 手写一个博客平台~第一天
- Handwriting a blog platform ~ first day
猜你喜欢
![[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+](/img/d8/90116f967ef0f5920848eca1f55cdc.png)
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+

【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21

Shell common script: check whether a domain name and IP address are connected

clickhouse笔记03-- Grafana 接入ClickHouse

Make a general cascade dictionary selection control based on jeecg -dictcascadeuniversal

Shell常用脚本:检测某域名、IP地址是否通

Convolutional neural network model -- alexnet network structure and code implementation

Zero basic learning canoe panel (16) -- clock control/panel control/start stop control/tab control

【AI4Code】《CodeBERT: A Pre-Trained Model for Programming and Natural Languages》 EMNLP 2020

并发编程 — 内存模型 JMM
随机推荐
Mid 2022 review | latest progress of large model technology Lanzhou Technology
Common operations for Yum and VIM
《富兰克林自传》修身
Simple understanding of flow
Shell Basics (exit control, input and output, etc.)
零基础学习CANoe Panel(15)—— 文本输出(CAPL Output View )
错误: 找不到或无法加载主类 xxxx
Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem
JS sorts according to the attributes of the elements in the array
[machine learning] experimental notes - emotion recognition
conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题
Redis可视化工具RDM安装包分享
卷积神经网络模型之——AlexNet网络结构与代码实现
Lu MENGZHENG's "Fu of broken kiln"
Docker学习 - Redis集群-3主3从-扩容-缩容搭建
Ministry of Public Security: the international community generally believes that China is one of the safest countries in the world
Handwriting a blog platform ~ first day
Selenium uses -- XPath and analog input and analog click collaboration
机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
clickhouse笔记03-- Grafana 接入ClickHouse