django 相关问题

Django是一个用Python编写的开源服务器端Web应用程序框架。它旨在减少创建复杂的数据驱动的网站和Web应用程序所需的工作量,特别关注更少的代码,无冗余以及比隐式更明确。

Django 的 on_commit 的用例是什么?

阅读本文档 https://docs.djangoproject.com/en/4.0/topics/db/transactions/#django.db.transaction.on_commit 这是 on_commit 的用例 with transaction.atomic(): # 外部原子,

回答 2 投票 0

如何手动禁用 Django 中的 cookies

我知道Django有处理cookie的应用程序,但我想手动完成。我有一个 Django 应用程序,只有两个 cookie:csrftoken 和 sessionid。 我想添加 cookie 同意禁令...

回答 1 投票 0

日期时间上的 Django ORM 过滤器可以理解具有“Z”偏移量的条目,但不能理解具有“+0000”偏移量的条目

我有一个镜像 Salesforce 数据库的 MySQL 数据库。 我的表有一列“createddate”,其中包含 ISO-8601 日期时间条目。有些条目的偏移量为“...

回答 1 投票 0

使用 Django 的微服务架构

我对使用 Django 创建微服务有一些疑问。 假设我们有一个在线商店或一个具有许多数据库请求和用户的更大系统。我想练习和模拟一个简单的

回答 1 投票 0

复制 Django 模板中的块内容

假设我在base.html中有以下代码: 其他扩展基础的模板...

回答 1 投票 0

dockerized Digital Ocean 设置中的 s3 上传超时

我在 Digital Ocean 上有一个 S3 兼容存储和一个 Droplet 服务器。我正在运行的 dockerized Django 应用程序正在尝试将静态资产同步到存储。这从 Droplet 服务器/Docker 失败

回答 1 投票 0

Django 拒绝连接 AWS 实例

我有一个 django 应用程序,它接近默认安装的应用程序。 在 settings.py 我有 调试=假 ALLOWED_HOSTS = ['*'] 我已经添加了一个网址 服务器/获取某物 我可以请求并返回...

回答 1 投票 0

运行 pip install --no-cache-dir -rrequirements.txt 安装但无法与 Docker 一起使用

我尝试在几个项目中使用 docker,一个是 Django,另一个是 python telegram bot;但在这两种情况下,问题是无论我如何将requirements.txt复制或安装到...

回答 1 投票 0

什么组件可能会发出“超出请求最大总标头大小”

我正在通过 REST api 将文档发布到 paperless-ngx 中。 对于某些 pdf 文档,API 会可靠地响应 {“detail”:“多部分表单解析错误 - 请求最大总标头大小 ex...

回答 1 投票 0

ModuleNotFoundError:当我尝试在 base.py 文件上写入“wagtail.contrib.modeladmin”时,没有名为“wagtail.contrib.modeladmin”的模块会出现此错误

从 django.contrib 导入 admin 从 .models 导入订阅者 @admin.register(订阅者) 类 SubscriberAdmin(admin.ModelAdmin): """订阅者的管理配置。&qu...

回答 1 投票 0

在 django ORM 中查询时如何将 char 转换为整数?

最近开始使用Django ORM。我想执行这个查询 从学生中选择student_id,其中student_id像“%97318%” order by CAST(student_id as UNSIGNED) desc; 其中 Student_id 是

回答 4 投票 0

django.db.migrations.exceptions.NodeNotFoundError:迁移accounts.0001_initial依赖项引用不存在的父节点

我正在尝试在heroku上部署我的项目,我正在使用django 3.1,但我无法做到这一点。由于迁移,我收到错误。请我谦虚地请求您花一些时间来解决这个问题......

回答 3 投票 0

我无法在 django 上创建新网址

我看过一些关于如何创建新的简单网址的教程,但由于某种原因它不起作用,看起来我没有注册任何网址,即使我注册了。 我创建了一个名为“登录”的应用程序并且

回答 1 投票 0

如何在同一端口上设置 Django 和 React 并处理路由?

问题 你好!我正在尝试设置我的 Django-React 应用程序,但我无法使用 React 路由器链接和 r...

回答 4 投票 0

Django 中的预填充字段无法正常工作

博客类(models.Model): blog_image=models.ImageField(upload_to="home/images") blog_title=models.CharField(max_length=150) slug=models.SlugField(max_length=200)

回答 1 投票 0

两个数据库列上的 Django 关系

我正在使用 django 与我无法控制的现有数据库进行交互。 数据库经常使用多个列来建立关系,并且所有这些列都具有唯一性......

回答 1 投票 0

反向时的m2m_changed实例

我有以下信号: @receiver(m2m_changed, 发送者=User.cars.through) def car_add_to_user(发件人、实例、操作、**kwargs): 如果在(“post_add”,)中执行操作: 缓存.删除...

回答 1 投票 0

我尝试使用 pip install django 安装 django 但仍然收到此错误任何评论

从 django.core.management 导入execute_from_command_line ModuleNotFoundError:没有名为“django”的模块 上述异常是导致以下异常的直接原因: 追溯(最近的...

回答 3 投票 0

boto.exception.S3ResponseError:S3ResponseError:403禁止

我试图让 django 将静态文件上传到 S3,但我收到了 403 禁止错误。 堆栈跟踪: 回溯(最近一次调用最后一次): 文件“manage.py”,第 14 行,位于 我试图让 django 将静态文件上传到 S3,但我收到了 403 禁止错误。 堆栈跟踪: Traceback (most recent call last): File "manage.py", line 14, in <module> execute_manager(settings) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager utility.execute() File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv self.execute(*args, **options.__dict__) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute output = self.handle(*args, **options) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle return self.handle_noargs(**options) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 89, in handle_noargs self.copy_file(path, prefixed_path, storage, **options) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 184, in copy_file if not self.delete_file(path, prefixed_path, source_storage, **options): File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 115, in delete_file if self.storage.exists(prefixed_path): File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/storages/backends/s3boto.py", line 209, in exists return k.exists() File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/boto/s3/key.py", line 391, in exists return bool(self.bucket.lookup(self.name)) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/boto/s3/bucket.py", line 143, in lookup return self.get_key(key_name, headers=headers) File "/home/levi/Projects/DoneBox/.virtualenv/local/lib/python2.7/site-packages/boto/s3/bucket.py", line 208, in get_key response.status, response.reason, '') boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden 设置.py: import os DIRNAME = os.path.dirname(__file__) # Django settings for DoneBox project. DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', '[email protected]'), ) MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': os.path.join(DIRNAME, "box.sqlite"), # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. # On Unix systems, a value of None will cause Django to use the same # timezone as the operating system. # If running in a Windows environment this must be set to the same as your # system time zone. TIME_ZONE = 'America/Denver' # Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'en-us' SITE_ID = 1 # If you set this to False, Django will make some optimizations so as not # to load the internationalization machinery. USE_I18N = True # If you set this to False, Django will not format dates, numbers and # calendars according to the current locale USE_L10N = True # Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/home/media/media.lawrence.com/media/" MEDIA_ROOT = '' # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/" MEDIA_URL = "d1eyn4cjl5vzx0.cloudfront.net" # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" STATIC_ROOT = os.path.join(DIRNAME, "static") # URL prefix for static files. # Example: "http://media.lawrence.com/static/" STATIC_URL = "d280kzug7l5rug.cloudfront.net" # URL prefix for admin static files -- CSS, JavaScript and images. # Make sure to use a trailing slash. # Examples: "http://example.com/static/admin/", "/static/admin/". ADMIN_MEDIA_PREFIX = '/static/admin/' # Additional locations of static files STATICFILES_DIRS = ( # Put strings here, like "/home/html/static" or "C:/www/django/static". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. os.path.join(DIRNAME, "main", "static"), ) # List of finder classes that know how to find static files in # various locations. STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'django.contrib.staticfiles.finders.DefaultStorageFinder', ) # Make this unique, and don't share it with anybody. SECRET_KEY = '<snip>' # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', 'django.template.loaders.eggs.Loader', ) MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) ROOT_URLCONF = 'DoneBox.urls' TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. os.path.join(DIRNAME, "main", "templates"), os.path.join(DIRNAME, "templates"), os.path.join(DIRNAME, "basic", "blog", "templates"), ) INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sitemaps', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin documentation: 'storages', 'django.contrib.admindocs', 'main', 'contacts', 'piston', 'registration', # 'contact_form', 'basic', 'basic.blog', ) # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error. # See http://docs.djangoproject.com/en/dev/topics/logging for # more details on how to customize your logging configuration. LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler' } }, 'loggers': { 'django.request': { 'handlers': ['mail_admins'], 'level': 'DEBUG', 'propagate': True, }, 'django.db.backends': { 'handlers': ['mail_admins'], 'level': 'DEBUG', 'propagate': True, } } } DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' AWS_ACCESS_KEY_ID = '<snip>' AWS_SECRET_ACCESS_KEY = '<snip>' STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage' AWS_STORAGE_BUCKET_NAME = "donebox-static" STATIC_FILES_BUCKET = "donebox-static" MEDIA_FILES_BUCKET = "donebox-media" ACCOUNT_ACTIVATION_DAYS = 7 EMAIL_HOST = "email-smtp.us-east-1.amazonaws.com" EMAIL_HOST_USER = '<snip>' EMAIL_HOST_PASSWORD = '<snip>' EMAIL_PORT = 587 EMAIL_USE_TLS = True TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.contrib.messages.context_processors.messages", "DoneBox.main.context_processors_PandC", ) 要求.pip: django==1.3 django-storages==1.1.4 django-registration==0.8 django-piston==0.2.3 django-tagging==0.3.1 django-extensions==0.8 BeautifulSoup==3.2.1 boto==2.4.1 mysql-python==1.2.3 tweepy==1.9 feedparser==5.1.2 pycrypto==2.6 我正在使用 Amazon IAM 作为特定密钥 ID 和访问密钥,但刚刚遇到了相同的 403 Forbidden...事实证明,您需要授予针对存储桶根及其子对象两者的权限: { "Statement": [ { "Principal": { "AWS": "*" }, "Effect": "Allow", "Action": "s3:*", "Resource": ["arn:aws:s3:::bucket-name/*", "arn:aws:s3:::bucket-name"] } ] } 我建议您尝试单独测试您的 AWS 凭证,以验证凭证是否确实具有在 S3 存储桶中读取和写入数据的权限。 以下应该有效: >>> import boto >>> s3 = boto.connect_s3('<access_key>', '<secret_key>') >>> bucket = s3.lookup('donebox-static') >>> key = bucket.new_key('testkey') >>> key.set_contents_from_string('This is a test') >>> key.exists() >>> key.delete() 您应该对另一个存储桶(“donebox-media”)尝试相同的测试。 如果有效,则权限正确,问题出在 Django 存储代码或配置中。 如果失败并返回 403,则: access_key/secret_key 字符串不正确 access_key/secret_key 正确,但该帐户没有写入存储桶所需的权限 希望有帮助。 请报告您的发现。 我也遇到了同样的问题,最后发现真正的问题是服务器时间。 它配置错误,AWS 以 403 FORBIDDEN 响应。 使用 Debian,您可以使用 NTP 自动配置: ntpdate 0.pool.ntp.org 如果您的机器时间设置不正确也会发生这种情况 如果这对任何人都有帮助,我必须添加以下配置条目才能使 collectstatic 正常工作而不返回 403: AWS_DEFAULT_ACL = '' 也有可能使用了错误的凭据。验证: import boto s3 = boto.connect_s3('<your access key>', '<your secret key>') bucket = s3.get_bucket('<your bucket>') # does this work? s3 = boto.connect_s3() s3.aws_access_key_id # is the same key being used by default? 如果没有,请查看 ~/.boto、~/.aws/config 和 ~/.aws/credentials。 这是一个具有最小权限的改进。 在所有情况下,正如所讨论的,其他地方s3:ListAllMyBuckets对于所有存储桶都是必要的。 在默认配置中,django-storages 会将文件上传到具有公共读取权限的 S3 - 请参阅 django-storages Amazon S3 后端 试验和错误表明,在此默认配置中,仅需要两个权限:首先上传文件,然后将该对象的权限设置为公共。 无需执行任何其他操作,因为从那时起,无论如何,该对象的读取都是公开的。 IAM 用户策略 - 公开读取(默认): s3:PutObject 让对象公开可读并不总是可取的。这是通过在设置文件中设置相关属性来实现的。 Django 设置.py: s3:PutObjectAcl 然后不再需要{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:ListAllMyBuckets", "Resource": "arn:aws:s3:::*" }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl" ], "Resource": "arn:aws:s3:::bucketname/*" } ] } ,最低权限如下: IAM 用户策略 - 私有: ... AWS_DEFAULT_ACL = "private" ... 另一种避免自定义策略并使用 AWS 预定义策略的解决方案: 为您的 S3 用户添加 S3 完全访问权限。 IAM / 用户 / 权限 和 附加策略 添加策略 “AmazonS3FullAccess” 也许您实际上无权访问您尝试查找/获取/创建的存储桶.. 请记住:存储桶名称在整个 S3 生态系统中必须是唯一的,因此,如果您尝试访问(查找/获取/创建)名为“test”的存储桶,您将无法访问它。

回答 9 投票 0

使用 Python 查找星期一的日期

如何使用 Python 根据当前日期查找上周一的日期?我想也许我可以使用: datetime.weekday() 来做到这一点,但我陷入困境。 我主要是想找到tod...

回答 8 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.