![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Django
WikiLeake
IT爱好者
展开
-
Django xadmin一次性选择多图上传并展示
Django xadmin实现一次性选择多张图片并存储的一个方法, 以及详情页面多图展示代码结构图直接上源码models.py设置如下class picture(models.Model): imgs = UploaderImageField(max_length=500, verbose_name='活动图片', null=True, blank=True, upload_to='image/') #文件#单图的设置class Img(models.Model):原创 2022-01-23 12:23:54 · 1799 阅读 · 0 评论 -
解决Django xadmin 图片上传后在后台无法显示和下载,出现Page not found (404)错误
问题详情:使用xadmin作为管理后台时,图片上传后在后台无法显示和下载,出现Page not found (404)错误原因:media路由没有正确配置解决方法settings.py中的设置:# 设置上传文件的路径MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #media即为图片上传的根路径urls.py中的设置from django.urls import path,re_pathfrom d原创 2022-01-23 09:42:19 · 1617 阅读 · 0 评论 -
django报错File “manage) from exc ^ SyntaxError: invalid syntax
Django报错: File "manage.py", line 14 ) from exc ^SyntaxError: invalid syntax出现此问题的原因是没有使用虚拟环境运行此项目。解决的方法:进入虚拟环境后重新运行此项目。s原创 2021-05-29 21:11:09 · 1006 阅读 · 0 评论 -
ubuntu将Django项目挂起在后台命令
使用如下命令,即可将Django项目在后台挂起,免杀死nohup python manage.py runserver 0.0.0.0:8000 &关闭终端后项目仍然可以继续运行原创 2021-05-17 19:35:23 · 417 阅读 · 0 评论 -
成功解决django迁移过程中出现的 return int(value) ValueError: invalid literal for int() with base 10: ‘‘错误
在django迁移过程中可能出现如下错误 Applying users.0006_auto_20210516_2004...Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/home/xiaobai/.virtualenvs/vtbd1/lib/python3.6/site-packages/dj原创 2021-05-16 21:18:01 · 613 阅读 · 0 评论 -
Django2实现word文件的下载功能
Django实现文件的下载的三种方式https://www.jb51.net/article/137790.htmDjango2实现word文件的下载功能(docx文件),可以文件具有中文字符的情况前端代码{% load staticfiles %}<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title>原创 2021-05-12 10:07:53 · 438 阅读 · 1 评论 -
django模板系统的使用
Django模板系统学习网站https://www.runoob.com/django/django-template.html原创 2021-05-07 09:46:48 · 81 阅读 · 0 评论 -
成功解决Django运行中出现的without providing an app_name is not supported.错误
Django运行中出现了如下错误:django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_原创 2021-05-04 16:08:42 · 163 阅读 · 0 评论 -
Django前端页面显示时间年月日,不显示时分秒
Django前端页面显示时间年月日,不显示时分秒的方法:格式:2021-04-21{{ time|date:'Y-m-d' }}原创 2021-04-21 17:10:00 · 1547 阅读 · 0 评论 -
Django实现登录系统后才能进入某些页面的两种方法
Django实现不登录的情况下不能进入某些页面的功能# views.pyfrom django.contrib.auth.decorators import login_required# 在类之前需要添加以下两行代码from django.utils.decorators import method_decorator@method_decorator(login_required,name='dispatch')class LoginView(View): def get(self,原创 2021-04-20 17:22:20 · 2560 阅读 · 0 评论 -
成功解决TypeError at /usercenter/ as_view() takes 1 positional argument but 2 were given
django报错TypeError at /usercenter/ as_view() takes 1 positional argument but 2 were given报错前url的写法是这样的path("usercenter/",UserCenterView.as_view,name="usercenter"),修改url的写法,解决此问题path("usercenter/",UserCenterView.as_view(),name="usercenter"),...原创 2021-04-20 14:21:14 · 2669 阅读 · 1 评论 -
Django中的queryset
queryset的链式调用posts = Post.objects.filter(status=1).filter(category_id=2).filter(title__icontains="xxx")原创 2021-04-19 21:19:27 · 68 阅读 · 0 评论 -
解决PackageNotFoundError的错误
使用Python-docx库时import os from docx import Documentfilnames = os .listdir('./')docx_files = [f for f in filnames if f.split('.')[-1] == "docx"]print(docx_files)docxs =[]for docx_file in docx_files: doc = Document(docx_file) for x in doc.parag原创 2021-04-17 16:50:31 · 5095 阅读 · 1 评论 -
ajax的初步学习
#引入juqery<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.js"></script><input id= "title" type="text" name = "title"><!--搜索数据的ajax请求,使用的是GET请求--><script> function AjaxSend() { $.ajax({原创 2021-04-16 08:53:01 · 91 阅读 · 0 评论 -
成功解决TypeError: ‘ManyRelatedManager‘ object is not iterable
Django前端页面显示多对多模型时出现如下错误TypeError: 'ManyRelatedManager' object is not iterable需要使用for标签遍历{% for a in dangqian.ziduan.all %}{{a.yinyongziduan}} {% endfor %}原创 2021-04-13 15:31:13 · 1800 阅读 · 0 评论 -
成功解决:‘WSGIRequest‘ object has no attribute ‘get‘
Django在获取表单内容时出现了'WSGIRequest' object has no attribute 'get'以前的view.py的方法zhivalue = request.get("content")原因是在views中获取这个字段的方法是错误的修改为:zhivalue = request.POST.get("content")...原创 2021-04-12 15:22:32 · 3721 阅读 · 0 评论 -
Django前端页面解析数据库中的html数据
加上 {% autoescape off %}和{% endautoescape off %}代码如下{% autoescape off %}{{ 要显示的数据}}{% endautoescape off %}原创 2021-04-11 21:55:57 · 274 阅读 · 0 评论 -
django报错django.db.migrations.exceptions.InconsistentMigrationHistory: Migration xadmin.0002_log is a
Django在使用user时,报了如下的错误django.db.migrations.exceptions.InconsistentMigrationHistory: Migration xadmin.0002_log is applied before its dependency users.0001_initial on database 'default'.解决的方法:第一种方法:删除数据库中的所有表并进行迁移第二种方法:删除数据库,新建一个数据库,之后进行迁移python manage.原创 2021-04-10 15:23:25 · 365 阅读 · 0 评论 -
django xadmin的安装及出错的解决方法
下载以及配置下载链接:https://github.com/sshwsfc/xadmin/tree/django2 并将其中的xadmin解压后放入extra_app中,并进行如下的配置:settings.py中的设置import syssys.path.insert(0,os.path.join(BASE_DIR, 'extra_apps'))INSTALLED_APPS = ['xadmin', 'crispy_forms',]出错: from future.utils import it原创 2021-03-14 17:27:03 · 2212 阅读 · 0 评论 -
Django手动给项目重命名
实现给Django项目重命名,至少需要修改一下六个地方:1.项目名称:直接在根目录进行修改2.settings.py中两项:WSGI_APPLICATION = 'ExamOnline.wsgi.application'ROOT_URLCONF = 'ExamOnline.urls'3.urls.pyfrom ExamOnline.settings import MEDIA_ROOT4.wisg.pyos.environ.setdefault("DJANGO_SETTINGS_MODULE"原创 2020-07-09 15:20:19 · 1998 阅读 · 0 评论 -
django获取前端button事件
<form action="" method="post">{% csrf_token %}<input type="hidden" name="shoucang" value=1> <button name="shoucang1">shoucang<button></form>思路是将前端form表单第一个input框隐藏,填入默认值,提交按钮类型为submmit,Django views.py中post请求获取name="shouc原创 2020-05-21 10:49:20 · 7796 阅读 · 2 评论 -
django.db.utils.OperationalError: (1426
Django开发中报错:django.db.utils.OperationalError: (1426, “Too big precision 100 specified for 'price'. Maximum is 65.”)原始models.py语句:price = models.DecimalField(max_digits=100, decimal_places=2)解决方案:price = models.DecimalField(max_digits=65, decimal_place原创 2020-05-18 16:06:58 · 612 阅读 · 0 评论 -
django开发中报错1366, Incorrect string value
Django开发中出现如下错误:(1366, "Incorrect string value: '\\xD0\\x9A\\xD0\\xB0\\xD1\\x82...' for column 'name' at row 1")解决方案创建数据库时使用utf-8,编码:create database 数据库名 default charset utf8 collate utf8_general_ci;...原创 2020-05-18 16:04:17 · 1011 阅读 · 0 评论 -
Django No module named corsheaders等问题的解决方案
Django开发中出现:No module named 'corsheaders解决方案:pip install django-cors-headersDjango开发中出现:No module named 'rest_framework'解决方案:pip install djangorestframeworkDjango开发中出现:import mysql.connector ImportError: No module named 'mysql'解决方案:pip install原创 2020-05-18 15:33:04 · 2083 阅读 · 0 评论 -
Django中使用省市联动模型详解
本程序实现在Django中使用省市联动模型,并实现地址值允许输入一次的效果:1.前端引入##html中引入 {% if request.user.province %} <li class=" p_infor_city">地 址: <input type="text" value="{{request.us原创 2020-05-17 15:39:57 · 559 阅读 · 0 评论 -
Django实现考试系统优化3
本次优化将针对考题部分,实现计算考试所用时间,并实现没有填写地址不能进行测试的功能。models.pyfrom django.db import modelsfrom datetime import datetime# Create your models here.from django.db import modelsfrom users.models import UserProfile# Create your models here.from DjangoUeditor.model原创 2020-05-17 15:27:42 · 294 阅读 · 0 评论 -
ubuntu安装如Django,jupyter notebook所需的虚拟环境
1.安装django开发所需要的虚拟环境#安装virtualv包sudo apt-get install virtualenv #安装virtualvwrapper包sudo apt-get install virtualenvwrapper #运行virtualenvwrapper.shsource /usr/share/virtualenvwrapper/virtualenvwra...原创 2020-05-07 08:51:52 · 280 阅读 · 0 评论 -
workon: command not found处理方法
$ workon django$ workon: command not found以前安装了虚拟环境,但是使用workon时发现命令不存在,处理方法:重新激活虚拟环境 source /usr/share/virtualenvwrapper/virtualenvwrapper.sh ...原创 2020-05-06 22:36:04 · 2588 阅读 · 1 评论 -
django实现考试系统之考试页面优化一
时间:2020年5月4日22:25:02能够循环显示试题并且能够正确判断分数是多少,但无法实现答题卡效果,悬停与变蓝{% load staticfiles %}<!DOCTYPE HTML><html><head><meta http-equiv="content-type" content="text/html;charset=UTF-8"&...原创 2020-05-04 22:25:48 · 545 阅读 · 0 评论 -
django获取各种形式的表单内容
Django框架获取各种form表单数据1.Django中获取text,password 名字:<input type="text" name="name"><br><br> 密码:<input type="password" name="password">Form表单提交数据时使用的是post方式,所以在后端接收参数的时候需要先判断...转载 2020-04-27 19:18:59 · 1470 阅读 · 0 评论 -
ueditor在Django中的使用
在修改models.py中的字段后,需要进行数据库的迁移#urls.pypath('ueditor/',include('DjangoUeditor.urls' )),#adminx.pyclass QuestionAdmin(object): #可以显示的列 list_display=["subject","statments","op_A","op_B","op_C","o...原创 2020-04-27 17:14:07 · 409 阅读 · 0 评论 -
django的日志器引入的方法
在Django中引入日志功能:1.在settings.py中加入以下代码:LOGGING = { # 版本 'version': 1, # 是否禁用已存在的日志器 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'formrat': '...原创 2020-04-27 15:40:17 · 267 阅读 · 0 评论 -
django实现考试系统优化2
时间:2020年4月26日18:51:43Django实现考试系统优化:实现从数据库中读取分数,并且计算分数,修改数据分数的数据类型为IntegerField,判断用户是否已经进行了测试,如果进行了测试,则不能再次测试#models.py#user/models.pyclass UserProfile(AbstractUser): gender_choices = ( ...原创 2020-04-26 19:06:12 · 359 阅读 · 0 评论 -
Django中聚合表达式
来源:知了课堂聚合函数笔记:所有的聚合函数都是放在django.db.models下面。聚合函数不能够单独的执行,需要放在一些可以执行聚合函数的方法下面中去执行。比如aggregate。示例代码如下:result = Book.objects.aggregate(Avg("price"))聚合函数执行完成后,给这个聚合函数的值取个名字。取名字的规则,默认是filed+__+...原创 2020-04-25 18:38:50 · 150 阅读 · 0 评论 -
Django2中@login_required的用法
在开发中会遇到这样的需求:即在用户未登录时不对用户开放某些页面,这种需求在Django2中解决办法为:1.在settings.py中添加:#settings.pyAUTH_USER_MODEL = 'users.UserProfile'#这个根据登录URL的不同进行修改LOGIN_URL="login"#附:urls.py中的设置: path('login/',LoginV...原创 2020-04-25 16:58:57 · 861 阅读 · 0 评论 -
Django实现考试系统,并实现后台数据可视化
时间:2020年4月23日17:58:01Django实现考试系统#views.pyfrom django.shortcuts import renderfrom django.views.generic import Viewfrom django.http import HttpResponsefrom .models import Question,Exam,Subject,Ma...原创 2020-04-23 18:52:23 · 2099 阅读 · 2 评论 -
计算分数算法
1.最简单的计算分数correct_answer=["A","B","C"]my_answer=["A","C","D"]mark=0i=0while i<3: if correct_answer[i]==my_answer[i]: mark+=1 i=i+1print(mark) #运行结果:12.分模块计算correct_answer...原创 2020-04-21 20:21:30 · 1210 阅读 · 0 评论 -
django考试试卷的实现【1】
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>这里是考试界面的首页</title></head><body><form action="" method="post"> ...原创 2020-04-21 18:10:18 · 1079 阅读 · 0 评论 -
django学习之表单
1.django表单1#forms.pyfrom django import formsclass MessageForms(forms.Form): title=forms.CharField(max_length=200,min_length=3,label="标题",error_messages={"min_length":"最好不能小于3个字符","max_length"...原创 2020-04-21 10:29:29 · 129 阅读 · 0 评论 -
设置iframe标签的大小
<ifrarme>标签的大小设置,Django填入<ifrarme>标签后自动设置大小<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style&g...原创 2020-04-19 21:26:06 · 4034 阅读 · 0 评论