当前位置:网站首页>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 .
边栏推荐
- Chapter5 : Deep Learning and Computational Chemistry
- Introduction to web security UDP testing and defense
- CONDA common commands: install, update, create, activate, close, view, uninstall, delete, clean, rename, change source, problem
- 程序员成长第二十七篇:如何评估需求优先级?
- Eccv2022 | transclassp class level grab posture migration
- Shell common script: judge whether the file of the remote host exists
- Ministry of Public Security: the international community generally believes that China is one of the safest countries in the world
- Make a general cascade dictionary selection control based on jeecg -dictcascadeuniversal
- Azure Devops(十四) 使用Azure的私有Nuget仓库
- 【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
猜你喜欢

How to use causal inference and experiments to drive user growth| July 28 tf67

从输入网址到网页显示

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

Machine learning strong foundation program 0-4: popular understanding of Occam razor and no free lunch theorem

卷积核越大性能越强?一文解读RepLKNet模型

Zero basic learning canoe panel (15) -- CAPL output view

2022 年中回顾 | 大模型技术最新进展 澜舟科技

Substance Designer 2021软件安装包下载及安装教程

零基础学习CANoe Panel(14)——二极管( LED Control )和液晶屏(LCD Control)
![Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]](/img/70/2b5130be16d7699ef7db58d9065253.png)
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]
随机推荐
B tree and b+ tree
ORAN专题系列-21:主要的玩家(设备商)以及他们各自的态度、擅长领域
深度学习的训练、预测过程详解【以LeNet模型和CIFAR10数据集为例】
好友让我看这段代码
[ai4code final chapter] alphacode: competition level code generation with alphacode (deepmind)
业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
【问题解决】org.apache.ibatis.exceptions.PersistenceException: Error building SqlSession.1 字节的 UTF-8 序列的字
【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.
Migrate PaloAlto ha high availability firewall to panorama
卷积核越大性能越强?一文解读RepLKNet模型
VIM tip: always show line numbers
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]
全球都热炸了,谷歌服务器已经崩掉了
牛客论坛项目部署总结
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)
[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]
程序员成长第二十七篇:如何评估需求优先级?
[today in history] July 25: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone
word样式和多级列表设置技巧(二)