Django 4.1.4 建立Article

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

前言

一、Django是什么?

二、安装

1.安装

2.建立项目和建立应用

数据模型

数据迁移

启动服务python manage.py runserver

​编辑

 路由

django的模板的开发

总结



前言

Django4.1.4 是python 的web应用程序的高级python框架 Django是python web开发界的重要地位,数据库支持可以选择MariaDB和MySQL


一、Django是什么?

Django4.1.4 是python 的web应用程序的高级python框架 Django是python web开发界的重要地位,数据库支持可以选择MariaDB和MySQL,ASGI支持

二、安装

1.安装

pip install django

pip install django
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: django in /Users/apple/PycharmProjects/djangoSample/venv/lib/python3.10/site-packages (4.1.3)

2.建立项目和建立应用

#django-admin startproject blog 

#直接运行就可以出现空的项目

#python manage.py article 

Django 4.1.4 建立Article_第1张图片

需要下项目的setting设置项目的入口

    'article.apps.ArticleConfig',

数据模型

修改models.py

建立用户和Acticle的model

from django.db import models

# Create your models here.

class User(models.Model):
    id = models.IntegerField(primary_key=True)
    username = models.CharField(max_length=30)
    email = models.CharField(max_length=30)

class Article(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=120)
    content = models.TextField()
    publish_date = models.DateTimeField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)


数据迁移

 python manage.py makemigrations
Migrations for 'article':
  article/migrations/0001_initial.py
    - Create model User
    - Create model Article


为了方便管理定义了后台UserAdmin和ArticleAdmin

from django.contrib import admin

# Register your models here.
from .models import User, Article


# admin.site.register(Post)


@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ('username', 'email')
    list_filter = ('username', 'email')
    search_field = (['username', 'email'])


@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    list_display = ('title', 'content', 'publish_date')
    list_filter = ('title',)
    search_field = ('title', )




启动服务python manage.py runserver

Django version 4.1.3, using settings 'demosite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.


Django 4.1.4 建立Article_第2张图片

 路由

在article 目建一个文件urls.py

from django.urls import path, re_path
from . import views

app_name = 'article'

urlpatterns = [
    # post views
    path('articles/list/', views.article_list),
    re_path(r'^articles/(?P[0-9]{4})/$', views.year_archive),
    re_path(r'^articles/(?P[0-9]{4})/(?P[0-9]{2})/$', views.month_archive),
    re_path(r'^articles/(?P[0-9]{4})/(?P[0-9]{2})/(?P[\w-]+)/$', views.article_detail),



]

分别定义了list和查询的views 有4个路由文件

到views.py编写对应的函数代码

from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.

def article_list(request):
    return HttpResponse("archive__list function")


def year_archive(request, year):
    return HttpResponse(f"year_archive function year:{year}")


def month_archive(request, year, month):
    return HttpResponse(f"month_archive function year:{year} month:{month}")


def article_detail(request, year, month, slug):
    return HttpResponse(f'article_detail function year:{year} month:{month} slug:{slug}')

 可以看出参数已经传输过来

Django 4.1.4 建立Article_第3张图片

 直接就返回HttpResponse到页面上。

django的模板的开发

        


总结

你可能感兴趣的:(python实践大全,python,开发语言)