Reference

Environment

  • Python3

venv

# create an env in catalogue at present
E:\django_project> python -m venv env

# into env
env\Scripts\activate.bat

Django Framework

  • Model: the layer to store data or data definition

  • Template: the layer of service logic catalogue

  • Views: the layer of expression, A collection of web pages with the same functionality and templates

This is like MVC

python manage.py runserver

Model

I follow the tutorial nothing want to say yet.

Views

  • Web pages are derived from views

  • Returns an HttpResponse object containing the contents of the requested page

# ./article/migrations/view.py
# 导入 HttpResponse 模块
from django.http import HttpResponse

# 视图函数
def article_list(request):
return HttpResponse("Hello World!")
  • With the view function, you also need to configure URLconfs to associate the URL links requested by the user. In other words, the purpose of URLconfs is to map the URL to the view.
# ./article/migrations/urls.py
# 引入path
from django.urls import path
# 引入views.py
from . import views
# 正在部署的应用的名称
app_name = 'article'

urlpatterns = [
# 目前还没有urls
# path函数将url映射到视图
path('article-list/', views.article_list, name='article_list'),
]

Create Superuser

when I want to run python manage.py createsuperuse meet an error: django.db.utils.OperationalError: no such table: auth_user,

the solve method form stackoverflow :

./manage.py migrate

If you’ve just enabled all the middlewares etc this will run each migration and add the missing tables.

https://stackoverflow.com/questions/24682155/user-registration-with-error-no-such-table-auth-user

and then when I want to Add article post, there is the other one error:

UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

and I find a solve methods:

https://www.its404.com/article/SaRAku/95866963

you need to find 334 line in debug.py , and add encoding="utf-8"

def get_traceback_html(self):
"""Return HTML version of debug 500 HTTP error page."""
with Path(CURRENT_DIR, 'templates', 'technical_500.html').open(encoding="utf-8") as fh:
t = DEBUG_ENGINE.from_string(fh.read())
c = Context(self.get_traceback_data(), use_l10n=False)
return t.render(c)

and you will find the admin return the real error no such table: article_articlepost

and then I find the reason, it shocks me, you need use these commands when you add a new app

python manage.py makemigrations
python manage.py migrate

about these commands of mean, I found this pages

python manage.py makemigrations这个命令是记录我们对models.py的所有改动,并且将这个改动迁移到migrations这个文件下生成一个文件例如:0001文件,如果你接下来还要进行改动的话可能生成就是另外一个文件不一定都是0001文件,但是这个命令并没有作用到数据库,这个刚刚我们在上面的操作过程之后已经看到了。

而当我们执行python manage.py migrate 命令时,这条命令的主要作用就是把这些改动作用到数据库也就是执行migrations里面新改动的迁移文件更新数据库,比如创建数据表,或者增加字段属性。
————————————————
版权声明:本文为CSDN博主「lotusgrm」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hpu_yly_bj/article/details/78928089

I think I just forget to do

Template

  • create a template
  • insert configuration into setting
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',
],
},

},
]

Bootstrap 4

Bootstrap 4 Download

Now, I find an error again, it likes my pages are not being rendered.

The next day I find the reason, and I forget to add these commands.

my_blog/settings.py

...

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

nothing to say yet

markdown render

nothing to say yet

CSRF attack

Cross-site request forgery attack

what is Same-origin policy ?

CSRF token csrf_token

凡是重要的数据操作,都应该考虑带有 csrf 令牌的 POST 请求;

或者更简单的方法,数据查询用 GET,数据更改用 POST。

User login

python manage.py startapp userprofile
  • Session

  • Django的 if 模板语句

User Delete

@login_requiredPython装饰器

Reset Password

  • Business Process

简单邮件传输协议 (Simple Mail Transfer Protocol, SMTP) 是在Internet传输Email的协议标准。

SMTP是基于文本的协议。在其之上指定了一条消息的一个或多个接收者,然后消息文本会被传输。SMTP使用TCP端口25。

SMTP是一个“推”的协议(发送邮件),它不允许从远程服务器上“拉”来消息(接收邮件)。要接收邮件,客户端必须使用POP3IMAP

https://www.dusaiphoto.com/article/34/

Extends user message

Django包含一个“信号调度程序”,它可以在框架中的某些位置发生操作时,通知其他应用程序。简而言之,信号允许某些发送者通知一组接收器已经发生了某个动作。当许多代码可能对同一事件感兴趣时,信号就特别有用。

每次改动模型后都需要进行数据的迁移。

Search pages

Comment

get error: no such table: comment_comment, try:

python manage.py makemigrations
python manage.py migrate