Django urls.py error

1

Good morning,
I am trying to put the 'MEDIA_ROOT' in 'urls.py' but when I run the 'runserver' I get the following error:

  

File   "C: \ Users \ Manux \ 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

This is my 'urls.py':

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.views import inicio


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', 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)

Here is my 'settings.py':

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_env", "static_root")

STATIC_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_env", "media_root")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static_pro", "static"),
]

This is my directory tree:

I give you the complete error that the 'runserver' gives me.

  

Unhandled exception in thread started by Traceback (most recent call last): File   "C: \ Users \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ utils \ autoreload.py",   line 226, in wrapper       fn (* args, ** kwargs) File "C: \ Users \ Manux \ 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 \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ core \ management \ base.py",   line 385, in check       include_deployment_checks = include_deployment_checks, File "C: \ Users \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ core \ management \ base.py",   line 372, in _run_checks       return checks.run_checks (** kwargs) File "C: \ Users \ Manux \ 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 \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ core \ checks \ urls.py",   line 14, in check_url_config       return check_resolve (resolve) File "C: \ Users \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ core \ checks \ urls.py",   line 24, in check_resolver       for pattern in resolver.url_patterns: File "C: \ Users \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ utils \ functional.py",   line 35, in get       res = instance. dict [self.name] = self.func (instance) File "C: \ Users \ Manux \ 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 \ Manux \ Desktop \ pd110 \ lib \ site-packages \ django \ utils \ functional.py",   line 35, in get       res = instance. dict [self.name] = self.func (instance) File "C: \ Users \ Manux \ 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)

I hope your help, greetings and thanks in advance.

    
asked by manuxdjent 31.03.2017 в 16:33
source

2 answers

2

I found it hard to find the error, but I found it:

This is your code (I just copied and pasted it):

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(
    os.path.dirname(BASE_DIR), "static_env", "static_root")

STATIC_URL = '/media/'
MEDIA_ROOT = os.path.join(
    os.path.dirname(BASE_DIR), "static_env", "media_root")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static_pro", "static"),
]

Reviewing the Django code a bit, and I went to the exact line where the error is thrown, it's this : and I see that the only way that you get that error, is that you are not sending a variable with some value.

Now, if you see your code, you will see that you have STATIC_URL = '/static/' and below you have again STATIC_URL = '/media/' , which means that you never defined in your settings the variable MEDIA_URL = '/media/' , so django when creating it by default, it is empty ( '' ), and given this is that the error is generated. It goes without saying that to solve it you must change the second STATIC_URL = '/media/' by MEDIA_URL = '/media/'

Any questions, questions, comment:)

    
answered by 01.04.2017 / 17:05
source
1

Try changing this:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', 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)

For the official form of the documentation:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', inicio, name='inicio')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Remember that this configuration in production must be delegated to the server through the configuration provided by the hosting.

I hope I have helped you.

    
answered by 31.03.2017 в 17:13