My diretory looks something like this:
project
|_app
|_ media
|_ profile_pics
|_ 1x1.jpg
|_ templates
|_ urls.py
...
I already specified the MEDIA settings on the settings.py:
# This part is right below the BASE_DIR
MEDIA_DIR = os.path.join(BASE_DIR, 'media')
# This part is right at the end of the file
# Media
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'
On my urls.py:
from django.conf.urls.static import static
from pro_five import settings
from . import views
app_name = 'my_app'
urlpatterns = [
url('^$', views.index, name='index'),
url(r'^registration/', views.register, name='register'),
url(r'^login/', views.user_login, name='user_login'),
url(r'^logout/', views.user_logout, name='logout'),
url(r'^profile/', views.profile, name='profile')
]
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
And finally on my template:
<img src="media/profile_pics/1x1.jpg" alt="asdasdasd"><br>
I don't know why but it just doesn't show up. I followed a lot of solutions only but seems nothing to work for me. I'm relatively new to Django so please forgive my naiveness.
Any advices? Thanks a ton!
My bad.
I misplaced the codes that was supposed to go to the project's urls.py not to the app's urls.py.
Also it's this:
from django.conf import settings
from django.conf.urls.static import static
Not:
from django.conf.urls.static import static
from myproject import settings
Related
Hope you are all having a very good Friday!
I am running a django project on nginx, and my project is each time going into this url - https://sitename.com/internal-nginx-static-location_uat/project_name/static/images/success.svg
I didn't configure this internal-nginx-static, and If I load the same project on another website, it works fine, is it something I can handle in code, or I have make changes in server conf.
Here is my URL.py Files
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('', include('portal.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(DATA_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Examined the documentation of django and this post Django MEDIA_URL and MEDIA_ROOT but, I'm still having issues, at first It was a SQlight issue so, I updated to latest Django from Django 2 now I'm getting:
AttributeError: 'Settings' object has no attribute 'MEDIA_Root'
Settings.py
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Also tried this:
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Removed the comma at the end:
Bad Code:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
Good Code:
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I guess you actually imported wrong settings. Re-check your actually code. Instead of importing this (upper-case):
from django.conf import Settings
you should import this (lower-case)
from django.conf import settings
Also, as was pointed in comments, this is an error (or you incorrectly pasted the code here):
'MEDIA_Root'STATIC_URL = '/static/'
I have a Django server setup on development as well as the production server. The development server loads the static files but the production server gives 404 on loading (although it renders the URL).
I have already used the collectstatic method to accumulate my static files.
settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
urls.py (main_project)
from django.contrib import admin
from django.urls import path, include
from django.conf import settings # new
from django.conf.urls.static import static # new
urlpatterns = [
path('', include('stock_management.urls', namespace='stock_management')),
path('auth/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls),
]
# if settings.DEBUG: # new
# urlpatterns += static(settings.STATIC_URL,
# document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL,
# document_root=settings.MEDIA_ROOT)
urls.py (App: stock_management) :
from django.urls import path, include
from .views import *
from django.conf import settings
app_name = 'stock_management'
urlpatterns = [
# Stock:
path('', stock_list, name='homepage'),
path('stock/', stock_list, name='stock_list'),
path('stock/add', stock_create_view, name='add_stock'),
path('stock/<pk>/edit', stock_edit, name='stock_edit'),
# Item:
path('items/', item_list, name='item_list'),
path('item/<pk>/edit', item_edit, name='item_edit'),
path('item/<pk>/delete', item_delete, name='item_delete'),
# API
path('api/items', item_list_API, name='item_list_API'),
# Gallery:
path('items/gallery', item_gallery, name='item_gallery'),
]
# if settings.DEBUG:
# # test mode
# from django.conf.urls.static import static
# urlpatterns += static(settings.STATIC_URL,
# document_root=settings.STATIC_ROOT)
# urlpatterns += static(settings.MEDIA_URL,
# document_root=settings.MEDIA_ROOT)
I want my static files to load in the server also.
When you set DEBUG=False in settings.py Django stops serving static files. You need to configure a web server like Nginx for static files.
Here a helpful tutorial: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps
The structure of my project is this:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Home/
templates/
Home/
Home.html
Login/
templates/
Login/
Login.html
Application/
templates/
Application/
Application.html
Details.html
mysite/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('Home.urls', namespace="Home")),
url(r'^Application', include('Application.urls',
namespace="Application")),
url(r'^Login', include('Login.urls', namespace="Login")),
url(r'^User', include('User.urls', namespace="User")),
]
Home/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.Home, name = 'Home'),
]
Application/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'Application', views.Application, name = 'Application'),
]
The structure for the views.py(s) is the same for each app (with changes in names, of course).
Home/views.py
from django.shortcuts import render
def Login(request):
return render(request, 'Login/Login.html')
I have two links in my Home.html. One is supposed to direct a user to Application.html and the other, to Login.html. I tried to do the followings:
templates/Home/Home.html
Apply Now!
Apply Now!
Login
None of them work. No errors, no redirections, nothing.
What is weird about this whole thing is that url changes for Login and Application look different, even though the structure of both apps are the same:
The url changes to:
http://localhost:8000/ApplicationApplication.html
vs
http://localhost:8000/Login/Login.html.
Would greatly appreciate any help!
mysite / urls.py
url(r'^Application', include('Application.urls',
namespace="app")),
Application/Application.urls
urlpatterns = [
url(r'Application', views.Application, name = 'application'),
]
home.html
Apply Now!
Please change to retry
I am a newbie so I might have done something stupid. running python 3.3 and Django 1.6.2.
When I run the local server via command line, this is the error I receive "P/1.1 404 1712" and error on the browser is "module not found" and the exception location direct me urls.py line 22;
document_root=settings.STATIC_ROOT)
this is a part of urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'signups.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
This is how my settings.py looks:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/whattheheck/static/'
# Template location
TEMPLATE_DIRS = {
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "templates"),
}
if DEBUG:
MEDIA_URL = '/whattheheck/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "media")
STATICFLIES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "whattheheck", "static", "static")
)
Can someone help please?
You forgot one static in the import statement, see the documentation:
from django.conf.urls.static import static
# ^^^^^^ this one
Right now, it tries to use the static module as a function but obviously, it does not work. The error 'module' object is not callable is raised when you are trying to use a module object (for example os, sys or any third-party) as a callable (with a __call__ method).