当前位置:网站首页>Django 2 ----- database and admin
Django 2 ----- database and admin
2022-07-25 13:13:00 【WFForstar】
List of articles
One . The view function
(1). Basic concepts
The function of view is to receive and send Web request , It is mainly used to deal with the business logic of the website
(2). Develop the first view
stay polls/views.py Next , Write the following code
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse(" Welcome to the voting system !")
notes :request This parameter must be passed , What is it? I will introduce it later
After writing the view function , First in poll Configure distributed routing under the directory , Create a new one urls.py file , Write the following code
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
Go to the main directory again urls.py Include this route
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('index', include('poll.urls')),
]
Open it in a browser ”http://127.0.0.1:8000/index“, If it's not wrong , You should see the page 
Two . Configuration of database
Here we use MySQL database
Direct access to settings.py, Probably 76 The position of the line
First of all mysql Create the corresponding database in
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test1_0719', # Names can be chosen at will
'USER':'root',
'PASSWORD':'123456',
'HOST':'127.0.0.1', # Local loopback address
'PORT':'3306', # Default port number 3306
}
}
3、 ... and . Build a model
stay poll/models.py Write the following code :
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) # Integer types , Mapping to the database will become 11 Bit int type
def __str__(self):
return self.choice_text
Then execute the following command on the command line :
python3 manage.py makemigrations
python3 manage.py migrate
These two script commands are to tell MySQL database ,models A change has taken place .
Four . Admin modular
python manage.py createsuperuser
Then follow the command line
Type in the browser http://127.0.0.1:8000/admin/, You can go in superuser Interface

stay poll/admin.py Enter the following code
from django.contrib import admin
# Register your models here.
from . models import Question
admin.site.register(Question)
Refresh the page to get a new interface , Then click the plus sign to edit your own question .
Now we have a back-end page , But there's no front page , This is in Django—3 We will talk about .
边栏推荐
- [机器学习] 实验笔记 – 表情识别(emotion recognition)
- [problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.
- Docekr学习 - MySQL8主从复制搭建部署
- 业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
- 【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】
- 好友让我看这段代码
- 深度学习的训练、预测过程详解【以LeNet模型和CIFAR10数据集为例】
- Mysql 远程连接权限错误1045问题
- 错误: 找不到或无法加载主类 xxxx
- ESP32-C3 基于Arduino框架下Blinker点灯控制10路开关或继电器组
猜你喜欢

【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)

【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.
![[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.](/img/00/65eaad4e05089a0f8c199786766396.png)
[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.

【OpenCV 例程 300篇】239. Harris 角点检测之精确定位(cornerSubPix)

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

massCode 一款优秀的开源代码片段管理器

Zero basic learning canoe panel (14) -- led control and LCD control

如何理解Keras中的指标Metrics

卷积神经网络模型之——VGG-16网络结构与代码实现

程序的内存布局
随机推荐
【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
MySQL remote connection permission error 1045 problem
[机器学习] 实验笔记 – 表情识别(emotion recognition)
基于JEECG制作一个通用的级联字典选择控件-DictCascadeUniversal
Mid 2022 review | latest progress of large model technology Lanzhou Technology
Handwriting a blog platform ~ first day
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
0720RHCSA
录制和剪辑视频,如何解决占用空间过大的问题?
业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
卷积神经网络模型之——AlexNet网络结构与代码实现
简单了解流
State mode
全球都热炸了,谷歌服务器已经崩掉了
Chapter5 : Deep Learning and Computational Chemistry
【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
Zero basic learning canoe panel (14) -- led control and LCD control
业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem