Django: updating file - django

I'm trying to upload a file but once it's uploaded I'm not able to open it since it shows an error saying that there's no an object with that Primary key. I think the error(s) should be in urls or in the path (MEDIA_ROOT) but can't find out what is wrong.
Any suggestion?
setings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "media/"
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static")
]
models.py
class Foo(models.Model):
file = models.FileField(upload_to="file_folder/", blank = True, null = True)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
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,
}),
)
--EDIT--
I'm using the Admin interface so there's no Views.py
And the traceback is not shown, only the following error:
ERROR
Request Method: GET
Request URL:http://localhost:8000/admin/db_personal/foo/3/media/file_folder/django.pdf/
foo object with primary key u'3/media/file_folder/django.pdf' does not exist.

MEDIA_URL needs to be an absolute path, like STATIC_URL:
MEDIA_URL = "/media/"

Related

Refused to apply style because its MIME type ('text/html') in django and next js

hello i did everything with integration but nothing seems working,its again saying the same error
all next js files are default no changes
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'frontend/.next/static/css')
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.test)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
pls help me find a solution to this problem i have done everything that i can do with this integration

I am not able to get that why this error. And what is the solution for this

I am beginner so please bear silly questions
if i remove highlighted lines 1,2,3(+ static line) my localhost it shows normal django homepage but after adding these highlighted lines, it shows error
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in portfolio.urls, Django tried these URL patterns, in this order:
admin/
^media/(?P<path>.*)$
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
but admin page loads with no problem
#urls.py file
from django.contrib import admin
from django.urls import path
from django.conf import settings **<----- 1**
from django.conf.urls.static import static **<----- 2**
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ** <--- 3**
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
You are trying to access root page of your app.
Your main urls.py, should look like this
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = = [
path('admin/', admins.site.urls),
path('', include("exampleapp.urls"),
]
Create another app, which will handle your root page using python manage.py startapp exampleapp
in exampleapp/urls.py
from django.urls import path
from . import views
app_name = "exampleapp"
urlpatterns = [
path('', views.index, name="index)
]
In exampleapp/views.py
from django.shortcuts import render
def index(request):
return render(request, "index.html", {})
Basically, in your app's root urls.py, you have not specifified the '/' route of your app or what you can call is the index of the app. You need to do that and the above is an example of it.
In your settings.py, add this, if you haven't:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
https://docs.djangoproject.com/en/2.2/howto/static-files/

Django : TypeError: static() got an unexpected keyword argument 'document_root'

I am trying to view my uploaded image through web-browser/DRF-browsable API. So I added MEDIA_URL and MEDIA_ROOT to my setting.py file. When I trying to run the code , it showing TypeError: static() got an unexpected keyword argument 'document_root' . Here is my relevant portions of code,
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR,'Images')
MEDIA_URL = '/Images/'
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.templatetags.static import static
urlpatterns = [
# Examples:
# url(r'^$', 'project.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', index),
url(r'^health$', health),
url(r'^admin/', include(admin.site.urls)),
url(r'^sample/',sampelview),
url(r'myapp/',include(myRouter.urls)),
url(r'^test/$', testRequest),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
This error is due to you are imported wrong References of static.Try to use from django.conf.urls.static import static instead of from django.templatetags.static import static

MEDIA_URL path doesn't show up

I have a problem with my static and media settings, so my uploaded images doesn't show up on my site page.
In my "settings.py" I have:
MEDIA_ROOT = (
os.path.join(BASE_DIR, '..', 'static', 'media')
)
MEDIA_URL = '/media/'
STATIC_ROOT = (
os.path.join(BASE_DIR, '..', 'static'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
In my "models.py" I have:
expert_photo = models.ImageField(upload_to='profiles')
Some "urls.py":
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^experts/all/$', 'expert.views.experts', name='experts'),
url(r'^experts/get/(?P<expert_id>\d+)/$', 'expert.views.expert', name='expert'),
)
And after all of that, when I go to my page, I see, that the picture have link, like this:
http://127.0.0.1:8000/static/profiles/i_vagin.jpg
But it must be:
http://127.0.0.1:8000/static/media/profiles/i_vagin.jpg
So how can I fix that?
Media files are not, by default, served during development. To enable media file serving, you need to add the following code in your urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Source: Django docs
Update
You'll also need to access images in your templates like this: {{ expert_photo.url }}

referencing to MEDIA_ROOT without hardcoding local directory

How do I change MEDIA_ROOT so its not a hardcoded directory value?
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = '/Users/blah/djangoproj/abc/abc/media/static/'
.py file
from django.conf import settings
print settings.MEDIA_ROOT
urls.py
from django.utils.translation import ugettext_lazy as _
from django.conf.urls.defaults import *
from django.conf import settings
urls = (...
(r'^%s(?P<path>.*)$'%settings.MEDIA_URL, 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
})
This should allow you to move your project easily between folders:
if your path is /home/user/projects/app/static/media/
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(SITE_ROOT, 'static/media')