我有一个关于将django应用程序放入"apps"子目录的问题.我在project_root中有一个名为"faktura"的应用程序.我不喜欢它在那里的事实,我想将我的所有应用程序存储在"apps"子目录下.
所以,我发现我可以将python路径扩展到"apps"子目录,所以在查看了互联网后,我将这个字符串添加到了settings.py:sys.path.insert(0,os.path.join(PROJECT_PATH,"应用")).然后我将应用程序添加到INSTALLED_APPS,如"faktura".一切顺利,直到我将url(r'^ faktura /',include('faktura.urls'))添加到根目录中的urls.py.从那以后,Django抛出错误信息"No module named faktura"full taceback就在这里:http://dpaste.com/737380/
这里有什么问题,为什么只有urls.py无法找到应用程序?如果我把它添加到PATH中它是否找不到这个应用程序?我花了一个上午试图弄清楚什么是错的,现在我需要你的帮助.
我有一个渲染图像的模板:
{% load staticfiles %}
<img src="{% static "img/logo.png" %}" alt="My image"/>
Run Code Online (Sandbox Code Playgroud)
图像链接已断开,但它指向:
localhost/static/img/logo.png
Run Code Online (Sandbox Code Playgroud)
我需要为static_root,static_url和STATICFILES_DIRS设置哪些值才能使此图像正确显示?
这是我的目录结构:
myprojectname(顶级)
--- myprojectname
--- --- myproectname
--- --- ---设置
--- --- --- --- base.py(setting.py)
- - - - 静态的
--- --- --- img
这是我在设置中的静态配置:
STATIC_ROOT = '/Users/myuser/myprojectname/myprojectname'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
#normpath(join(SITE_ROOT, 'static')),
os.path.join(BASE_DIR, "static"),
'/Users/myuser/myprojectname/myprojectname/static',
)
Run Code Online (Sandbox Code Playgroud)
这就是它所显示的:

我已经完成了一个收集站,这不起作用.
我一整天都在思考这个问题,但无法弄清楚问题所在。它发生在我将我的 Django 项目从一台 PC 复制到另一台之后。
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/username/opt/anaconda3/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/Users/username/opt/anaconda3/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS) …Run Code Online (Sandbox Code Playgroud) 有没有办法以编程方式将URL模式添加到Django而无需重新启动服务器?
或者有没有办法强制Django重新处理/缓存URL模式(URLconf)?
我创建了一个Django应用程序.我在应用程序中有一个注册页面(简单的HTML表单),并且在注册时有一个电子邮件字段.现在我想在用户注册时实施电子邮件验证.就像向用户发送电子邮件一样(通过注册表格发送电子邮件).通过谷歌搜索我发现有一个Django方法'send_email'可以用于上述.但作为一名Django新手,我无法理解为此而创建的更改和功能.有人可以帮我做这个或指出一些可以帮助我的教程.或者除了Django的'send_email'方法之外还有其他任何方法来实现它.任何帮助,将不胜感激
我会在这里粘贴我现有的代码.
VIEWS.PY
def registrationForm(request):
if request.method == "POST":
firstName = request.POST.get("firstName")
lastName = request.POST.get("lastName")
email = request.POST.get("email")
password = request.POST.get("password")
sex = request.POST.get("sex")
birthday = request.POST.get("birthday")
print request.POST.get("sex")
UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()
return render_to_response('registrationForm.html')
return render_to_response("registrationForm.html")
def login(request):
if request.POST:
email=request.POST.get("username")
password = request.POST.get("password")
print email
print password
user = UniversityDetails.objects.filter(email=email,password=password)
print user
if(not user):
return render_to_response("registrationForm.html",{'invalid': True })
else:
return render_to_response("login.html")
return render_to_response("registrationForm.html")
Run Code Online (Sandbox Code Playgroud)
registrationForm.html
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$("#datepicker").datepicker();
});
</script>
</head> …Run Code Online (Sandbox Code Playgroud) python django django-templates django-models django-settings
使用DEBUG = True,Django异常转储到stderr,这通常由Web服务器发送到旋转日志文件.
使用DEBUG = False,Django会将异常通过电子邮件发送到ADMINS =.
如何使用DEBUG = False保留DEBUG = True行为?
我已经阅读了如何在django站点上记录服务器错误,如何查看Django视图的错误日志以及如何在django站点上记录服务器错误.答案似乎涉及一些中间件.是否有可用的代码段,或者是否包含这些电池?
我刚刚使用python 2.7在OS X 10.8上安装了Django 1.6,但我在浏览教程时遇到了麻烦.设置通过运行创建名为mysite的服务器后:
django-admin.py startproject mysite
Run Code Online (Sandbox Code Playgroud)
然后我进入了mysite并跑了
python manage.py runserver
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 280, in execute
translation.activate('en-us')
File "/Library/Python/2.7/site-packages/django/utils/translation/__init__.py", line 130, in activate
return _trans.activate(language)
File "/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py", line 188, in activate
_active.value = translation(language)
File "/Library/Python/2.7/site-packages/django/utils/translation/trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE) …Run Code Online (Sandbox Code Playgroud) 在settings.py中导入自定义后端的正确方法是什么?我目前在settings.py中有以下内容:
AUTHENTICATION_BACKENDS = ('apps.apployment_site.auth.CustomAuth')
Run Code Online (Sandbox Code Playgroud)
其中apployment_site是应用程序,auth是文件名,CustomAuth是类名.
在我看来,我得到:ImportError: a doesn't look like a module path在我运行以下代码之后:
from django.contrib.auth import authenticate
from apployment_site import *
authenticate(username="username", password="password")
Run Code Online (Sandbox Code Playgroud)
这是我的完整settings.py:
"""Django settings for apployment project.
For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
# SECURITY WARNING: …Run Code Online (Sandbox Code Playgroud) 我想使用Django-Allauth,所以我安装如下,它在我的笔记本电脑localhost中完美运行; 但是当我在我的服务器中拉它时,遇到以下错误:
No module named 'allauth.account.context_processors'
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
# Django AllAuth
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Already defined Django-related contexts here
# `allauth` needs this from django
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
# `allauth` specific context processors
'allauth.account.context_processors.account',
'allauth.socialaccount.context_processors.socialaccount',
"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.core.context_processors.tz",
"django.core.context_processors.request",
"moolak.context_processors.image",
],
},
},
]
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的项目文件夹结构:
project
applications
__init__.py
app1
app2
app3
project
__init__.py
settings.py
Run Code Online (Sandbox Code Playgroud)
在我的settings.py我试图导入这样的应用程序:
INSTALLED_APPS = (
'django.contrib.admin',
...
'applications.app1',
'applications.app2',
'applications.app3',
)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试迁移其中一个应用程序,我会收到此错误:
./manage.py makemigrations applications.app1
App 'applications.app1' could not be found. Is it in INSTALLED_APPS?
Run Code Online (Sandbox Code Playgroud)
可能有什么不对?这种设置过去常用于django 1.6
django ×10
django-settings ×10
python ×7
django-admin ×1
django-apps ×1
django-urls ×1
macos ×1
packages ×1