Code Monkey home page Code Monkey logo

django_blog's Introduction

Django_blog

Relearn Django framework by constructing a blog app step by step

Notes:

之前做过几个课程项目使用的都是Django框架,但是都有一些拿来主义的感觉,没有对这个框架进行深入的了解,这里碰巧发现一个蛮不错的教程,在此重新跟着教程重新码一遍代码。

参考:

https://www.dusaiphoto.com/article/detail/2/

  • 进度

day1: 搭建开发环境(git 目录 my_blog project目录 app 目录),

# 创建project 
>django-admin startproject my_blog
# 查看效果
>cd my_blog 
>python manage.py runserver

# 创建APP
>python manage.py startapp article

# 注册APP(settings)
my_blog/settings.py
INSTALLED_APPS = [
    ...,
    'article',
]

# 配置访问路径(urls)
my_blog/urls.py
urlpatterns = [
    ...,
    path('article/', include('article.urls', namespace='article')),
]

article/urls.py (新建一个urls.py)
from django.urls import path
app_name = 'article'
urlpatterns = [
    # 后续views中的函数url
]

day2: 理解MTV模型,创建数据库,编写view.py部分代码(文章列表,详情),编写部分模板

MVT表达的意思和MVC是一样的

M = M 数据处理 => 数据库 Django通过models.py 映射到数据库 (ORM object relational mapping) V = C 控制器 => 业务处理 T = V 视图 => 页面显示

article/models.py
# 文章类
class ArticlePost(models.Model):
    ...  
    
# 数据库迁移  ->  每当对数据库进行了更改(添加、修改、删除等)操作,都需要进行数据迁移。
## 通过运行 makemigrations 命令,Django 会检测你对模型文件的修改,并且把修改的部分储存为一次迁移。
>python manage.py makemigrations
>python manage.py migrate

# 创建管理员账号,管理网站后台
>python manage.py createsuperuser

# 将ArticlePost注册到后台中, 通过注册可以将该模型交给网站后台进行管理(可以在开发阶段进行添加测试数据)
article/admin.py

from .models import ArticlePost
admin.site.register(ArticlePost)


# 编写view函数  view -> 函数 (接受request,返回response)
def article_list(request):
	...

def article_detail(request, id):
	...

# urlconf映射视图函数
path('article-list/', views.article_list, name='article_list')


base.html list.html ....
...

Django views are a key component of applications built with the framework. At their simplest they are a Python function or class that takes a web request and return a web response. Views are used to do things like fetch objects from the database, modify those objects if needed, render forms, return HTML, and much more.

day3: Form表单使用,发表文章、删除文章、修改文章

article/forms.py
class ArticlePostForm(forms.ModelForm):
    ...
    
article/views.py 
def article_create(request):
    ...
    
templates/article/create.html 写模板

# 删文章
def article_delete(request, id):
    ...
    
def article_update(request, id):
    ...

django_blog's People

Contributors

bin-go2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.