Is there any way to serve Media files when runserver --insecure
It serves only css and js and image but i need to serve media file with ...
They mention it's not possible with cachefile but no word about media.
You need to tell it to serve your media files. In your urls.py:
from django.conf import settings
if settings.DEBUG == False:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
Write:
import re
from urllib.parse import urlsplit
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.urls import re_path
from django.views.static import serve
def static(prefix, view=serve, **kwargs):
return [
re_path(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),
]
Then in urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Related
Django Post list/detail urls, domain.com/api/v1/1 and domain.com/api/v1 not found given the following url patterns, note that pk with value of 1 exists,
Project urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('posts.urls'))
]
Api urls
from django.urls import path
from .views import PostList, PostDetail
urlpatterns = [
path('<int:pk>/', PostDetail.as_view()),
path('', PostList.as_view()),
]
Try adding a slash to the end of api/v1
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/', include('posts.urls'))
]
I am getting page not found(404) error for votings app that I created from datacamp tutorial. I have checked my code to make sure it's free of errors. admin is working fine but other urls are not.
Here's urls.py code from the main application directory:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Here's urls.py from the votings app directory:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index, name='index'),
path('<int:question_id>/',views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
I am using django 2.0.5.
Thanks
Unless you've made a mistake copying the wrong urls.py for votings app, the problem has to be it.
This is the main urls.py of your project:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
FYI, according to the docs include() adds urls from your app directory's (in your case it's voting) urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.
And this is the urls.py of your votings app which is literally the copy of main urls.py:
from django.urls import include, path
from django.contrib import admin
urlpatterns = [
path('blog/', include('blog.urls')),
path('votings/',include('votings.urls')),
path('admin/', admin.site.urls),
]
Don't you see any problem here? There's no endpoint. Where's the associated view (function-based or class based) for this url?
I suggest writing a view in your views.py and test it out:
Votings app views.py:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Votings app urls.py:
from django.urls import include, path
from . import views
urlpatterns = [
path('home/', views.current_datetime, name='home'),
]
I have a django rest project which is built on Django1.7. I need to run it on Django 1.11. When i run
python manage.py migrate
The error is:
ImportError: No module named urls
on url.py line
url(r'^docs/', include('rest_framework_swagger.urls')),
I have already made modifications in url.py file to avoid patterns. The url.py file look like
from django.conf.urls import include,url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^grappelli/', include('grappelli.urls')),
url(r'^docs/', include('rest_framework_swagger.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'', include('gcm.urls')),
url(r'^', include('apps.account.urls')),
url(r'^', include('apps.vegetables.urls')),
url(r'^', include('apps.orders.urls')),
url(r'^', include('apps.listings.urls')),
url(r'^', include('apps.rating.urls')),
url(r'^', include('apps.faq.urls')),
url(r'^thank-you/$', TemplateView.as_view(template_name="thankyou.html"), name="thankyou"),
url(r'^/error/$', TemplateView.as_view(template_name="error.html"), name="error"),
url(r'^$', TemplateView.as_view(template_name="index.html"), name="home"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
'',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT})]
How could i run it?
In urls.py add this at the bottom,
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT
From Django 1.9 it does not support to add the URLs as a string, but it need to be imported as callable. So remove this from your urls.py.
urlpatterns += [ '', (r'^static/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT})]
I deployed a django app to heroku, using "git push heroku master". It works fine but i have problem with static files. I can't configure it. What i have to do to get it started? Can you help me guys?
settings.py
DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
base.html
<link rel='stylesheet' href='{% static "css/base.css" %}' />
urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
from accounts.views import (login_view, register_view, logout_view)
from timetable.views import home
urlpatterns = [
url(r'^timetable/', include("timetable.urls", namespace='timetable')),
url(r'^admin/', admin.site.urls),
url(r'^home/', register_view, name='register'),
url(r'^login/', login_view, name='login'),
url(r'^logout/', logout_view, name='logout'),
url(r'^$', home, name='controler'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Try this
import os
from django.conf import settings
from django.conf.urls import include, patterns, url
from django.contrib import admin
# all other necessary imports
admin.autodiscover()
BASE_DIR = os.path.dirname((__file__))
urlpatterns = [
# all my url() patterns
]
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)
I've read the documentation of the Django about serving static files, and it says you should use django.conf.urls.static.static function in order to server them.
Here is my code:
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from settings import STATIC_ROOT, STATIC_URL
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^api/v1/', include('news.rest_urls', namespace='rest_framework')),
static(STATIC_URL, STATIC_ROOT)
]
And this is the error I receive:
'list' object has no attribute 'regex'
You need to define your STATIC_URL and STATIC_ROOT in settings.py and add to your urlpatterns next:
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)