博客
关于我
Django 2.0 基础
阅读量:223 次
发布时间:2019-02-28

本文共 2218 字,大约阅读时间需要 7 分钟。

Django 项目配置与开发指南

1. 项目创建

1.1 创建项目

使用以下命令创建新的 Django 项目:

django-admin startproject myproject

进入项目目录:

cd myproject

1.2 创建应用

创建新应用(file 为示例):

python manage.py startapp file

1.3 启动服务器

启动开发服务器:

python manage.py runserver

2. 数据库设置

2.1 数据库迁移

创建或修改数据库迁移文件:

python manage.py makemigrations

执行迁移:

python manage.py migrate

2.2 数据库配置

settings.py 中配置数据库:

import pymysqlpymysql.install_as_MYSQLdb()DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'NAME': 'your_database_name',        'USER': 'your_user',        'PASSWORD': 'your_password',        'HOST': 'localhost',        'PORT': '3306',    }}

2.3 时区与语言

设置语言和时区:

LANGUAGE_CODE = 'zh-Hans'TIME_ZONE = 'Asia/Shanghai'

2.4 模板与静态文件

配置模板路径:

TEMPLATES = [    {        'BACKEND': 'django.template.backends.django.DjangoTemplates',        'DIRS': [os.path.join(BASE_DIR, 'templates')],        'APP_DIRS': True,        'OPTIONS': {            'context_processors': [                'django.template.context_processors.debug',                'django.template.context_processors.request',                'django.contrib.auth.context_processors.auth',                'django.contrib.messages.context_processors.messages',            ],        },    },]

静态文件配置:

STATIC_URL = '/static/'STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')

3. URL 管理

3.1 主项目 URL

urls.py 中:

from django.contrib import adminfrom django.urls import path, includeurlpatterns = [    path('admin/', admin.site.urls),    path('', include('file.urls', namespace='file')),]

3.2 应用 URL

在应用 fileurls.py 中:

from django.urls import pathfrom . import viewsapp_name = 'file'urlpatterns = [    path('', views.home, name='home'),    path('test/', views.test, name='test'),]

4. ORM 模型映射

4.1 创建模型

file/models.py 中定义模型:

from django.db import modelsclass FileModel(models.Model):    name = models.CharField(max_length=100)    description = models.TextField()    upload_date = models.DateTimeField(auto_now_add=True)

4.2 数据库迁移

执行上述迁移命令即可创建相应的数据库表。

5. 模板引入

5.1 BloclExtends

在需要扩展的模板中:

{% extends 'base.html' %}

5.2 静态文件加载

在模板中使用:

{% load static %}{{ static 'file/img/timg.jpg' }}

6. URL 引用

在模板中引用 URL:

访问首页

以上配置均可通过实际项目进行调整和扩展,确保开发流程的顺利进行。

转载地址:http://euxp.baihongyu.com/

你可能感兴趣的文章
NI笔试——大数加法
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>