2022年7月8日

Django: session详解

    cookie:Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份、进行 session 跟踪而储存在用户本地终端上的键值对数据   1.什么是session?   首先引入百度百科的解释:Session:在计算机网络应用中,称为“会话控制”。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。Session 对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可…
2022年7月7日

Django: 字符串中HTML防止转义/模版关闭转义的方法

1#.Python代码中 from django.utils.safestring import mark_safe def index(request): s = "<h1>index page</h1>" s = mark_safe(s) return render(request, "app01/index.html", {"h1Tag": s} #--------------- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>app01 index</title> </head> <body> {{ h1Tag }} </body> </html…
2022年7月6日

Django: 报错 django.urls.exceptions.NoReverseMatch: Reverse for 'param' with no arguments not found. 1 pattern(s) tried...

reverse() https://docs.djangoproject.com/en/2.2/ref/urlresolvers/#reverse If you need to use something similar to the url template tag in your code, Django provides the following function: reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)[source]¶ viewname can be a URL pattern name or the callable view object. For example, given the following url: from news import views path('archiv…
2022年7月6日

Django: 报错 'Specifying a namespace in include() without providing an app_name ’

1. 问题概述: 在ROOT_URLCONF的urls.py中使用了include方法,并且使用了namespace参数: urlpatterns = [ path('admin/', admin.site.urls), path('', include('book.urls', namespace='book')), ] 在启动项目时,会报错: File "/home/vubuntu/PycharmProjects/pythonProject/bookmanager/urls.py", line 21, in path('', include('book.urls', namespace='book')), File "/home/vubuntu/PycharmProjects/pythonProject/venv/lib/python3.8/site…
2022年7月6日

Django: python manage.py makemigrations - No changes detected

如果你更新了models.py里的类,想更新到数据表里。 python manage.py makemigrations 提示 No changes detected app\migrations\__init__.py文件打开是空的, 但如果把其删除,python manage.py makemigrations时就会提示No changes detected(无检查到更改)。 这种情况,在对应app\migrations目录下重建一个空的__init__.py,就可以了。 ############################################################## To create initial migrations for an app, run makemigrations and specify the app name.…
2022年6月29日

Django: How to use Django with FastCGI, SCGI, or AJP

原文:https://docs.djangoproject.com/en/1.8/howto/deployment/fastcgi/ Deprecated since version 1.7:FastCGI support is deprecated and will be removed in Django 1.9. Although WSGI is the preferred deployment platform for Django, many people use shared hosting, on which protocols such as FastCGI, SCGI or AJP are the only viable options. Note This document primarily focuses on FastCGI. Other protocols, such as SCGI a…
2022年6月28日

Django: ModuleNotFoundError: No module named 'models'

初次接触Django框架的编写,遇到了一个问题,即import出现了错误。工程结构如下: 在对admin.py文件进行管理员权限注册数据,时出现错误,代码如下: from django.contrib import admin from models import * admin.site.register(BookInfo) 发现出错: ModuleNotFoundError: No module named 'models' 解决: # 1.print(sys.path)查看系统路径,根据系统路径来确定导包路径 # 2.使用相对导入 ,修改代码如下: from django.contrib import admin from .models import * admin.site.register(BookInfo) 之后可以成功运行web服务器。…
2022年6月28日

Django: ModuleNotFoundError: No module named 'xxx'

(venv) vubuntu@vubuntu:~/PycharmProjects/pythonProject$ python DjangoProject/manage.py startapp DjangoApp (venv) vubuntu@vubuntu:~/PycharmProjects/pythonProject$ python DjangoProject/manage.py makemigration 提示: ModuleNotFoundError: No module named 'xxx' 解决办法: (venv) vubuntu@vubuntu:~/PycharmProjects/pythonProject$ cd DjangoProject (venv) vubuntu@vubuntu:~/PycharmProjects/pythonPro…