我正在使用udemy的教程。我是关于设置静态文件的部分。 https://www.udemy.com/probar-django-construir-una-aplicacion-web-en-python/learn/v4/overview就是这个链接。
在视频22上显示了如何配置静态文件。
我在教程中配置了所有内容。这比仅从文档,colins,大写'POST'中键入代码更进一步。同样,我的代码与教程完全一样。
关于如何阅读错误消息的任何评论,只看看有用的 这些东西也有很好的帮助。
视频是西班牙语,但页面和代码都是英文。
任何人都可以通过查看错误消息或查看来告诉我 看看可能出错的教程。
Unhandled exception in thread started by <function wrapper at 0x034034B0>
Traceback (most recent call last):
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\core\management\commands\runserver.py", line 121, in
inner_run
self.check(display_num_errors=True)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\core\management\base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\core\management\base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\core\checks\urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\urls\resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns",
self.urlconf_module)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\urls\resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\migel\Desktop\pd110\src\pd110\urls.py", line 32, in
<module>
urlpatterns + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
File "C:\Users\migel\Desktop\pd110\lib\site-
packages\django\conf\urls\static.py", line 24, in static
raise ImproperlyConfigured("Empty static prefix not permitted")
django.core.exceptions.ImproperlyConfigured: Empty static prefix not
permitted
my url.py
“”“pd110网址配置
The `urlpatterns` list routes URLs to views. For more information please
see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from boletin import views
#from boletin.views inicio
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^contact/$', views.contact, name='contact'),
url(r'^$', views.inicio, name='inicio'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
STATIC_URL = '/media/'
#/static/images/img1.jpg
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_pro", "static"),
#'/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_env",
"static_root")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_env",
"media_root")
答案 0 :(得分:0)
如果您前往消息来源并浏览the function提出该错误,则会启动:
# django/conf/urls/static.py
def static(prefix, view=serve, **kwargs):
...
if not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
因此,在代码中的某个时刻,您将第一个参数prefix
传递给false-y(如空字符串)。查看您的urls.py
,您有
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我猜想STATIC_URL
或MEDIA_URL
是空的。看起来你宣称是前者,所以可能是后者。
答案 1 :(得分:0)
url.py
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
settings.py
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
#/static/imagenes/img1.jpg
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_pro", "static"),
#'/var/www/static/',
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_env",
"static_root")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_env",
"media_root")
答案 2 :(得分:0)
嘿!!!似乎未创建根媒体文件夹,请先创建超级用户并创建作业,然后将自动创建该文件夹。 然后添加媒体网址“ + static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)”。
希望!会正常工作。
答案 3 :(得分:0)
我遇到了同样的问题,所以我通读了static.py
文件,它需要一个前缀...所以我做了这样的事情:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
然后我又重新启动并且没有错误...