Django how to change admin urls? - django

i have a problem. In my project i am uploading file, it gets saved perfectly fine. It is accessible. I save it to the static/assets/uploaded files, ten generate the name: time + original filename.
But in django admin page i can't access it:
(link that admin page gives me)
http://127.0.0.1:8000/admin/project/minipost/4/uploaded_files/1428326830_08_name.pdf/
The correct url would be:
http://127.0.0.1:8000/static/assets/uploaded_files/1428326830_08_name.pdf/
I have no idea how do i change or redirect or whatever the admin generated link to the correct one?
Thanks in advance.

The problem was indeed MEDIA_URL
I just had to set it correctly:
MEDIA_ROOT = '/home/work/django-work/workproject/static/'
MEDIA_URL = '/static/assets/'

Related

Create a 'dummy' URL path for frontend pictures rendering

I have application with DjangoRestFramework for backend and Vue.js for frontend.
My user can upload pictures and they are stored in an uploaded_media file.
Therefore I added + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to my urlpatterns.
But since it is a Single Page Application, I added re_path(r"^.*$", IndexTemplateView.as_view(), name="entry-point"), in my urlpatterns for everything to be redirected to my index.html.
My issue is since my pictures URL is fetched in this format: "http://127.0.0.1:8000/media/my_pic.jpg", it can't be rendered frontend because it's redirected to my entry-point.
So I made a 'dummy' URL path (path('media/', DummyView.as_view())) pointing to a dummy View:
class DummyView(TemplateView):
pass
And it works... just for the first picture. I am doing it right and missing something or I am going to the wrong direction?
If it helps, I find a better way than creating a 'dummy' view, I just change my entry-point URL this way:
re_path(r"^(?!media).*$", IndexTemplateView.as_view(), name="entry-point")
So everything except the URL starting with 'media' is redirected to the entry-point

Where is `LOGIN_REDIRECT_URL` located in django?

I have followed a couple tutorials that say to change LOGIN_REDIRECT_URL in the settings.py file to customize redirect, but I cannot find it anywhere. I also read that it would be deprecated in Django 1.10 (which I am using) but it is still listed in the docs.. Where is LOGIN_REDIRECT_URL located or am i supposed to customize another way?
LOGIN_REDIRECT_URL has a default value (/accounts/profile/) but it doesn't appear in settings.py by default, you need to add it in if you want to change it.
LOGIN_REDIRECT_URL = '/my_own_login_url'
or, even better, as of 1.5 you can use your named URL patterns:
LOGIN_REDIRECT_URL = 'some_app:login'
and I've checked the settings documentation for 1.10, 1.11, and dev, no deprecation warnings on any of them.

Running Django site with a URL prefix

I'm trying to put my Django site in a subpath, say www.example.com/mysite/ but I can't get it to work 100%.
I read up on the answer Django Apache Redirect Problem , where it is suggested to just change the site in the admin from www.example.com to www.example.com/mysite/, which is exactly what I want to do and it almost works. All urls in the main urls.py gets redirected properly, but everything in the includes drop the "mysite" directive when using the links in the templates (one example is {% url journal_index %} which after the change should go to www.example.com/mysite/journal but goes go www.example.com/journal/).
Hope this makes sense.
Cheers!
Try adding the FORCE_SCRIPT_NAME setting:
FORCE_SCRIPT_NAME = '/mysite'

Django: Making alias in urls.py for amazon s3?

I am encountering a problem in setting up the URLs in Django.
To serve my media files I have this Amazon S3 bucket:
https://somebucket.s3.amazonaws.com/
I set the media URL inside settings.py as follows:
MEDIA_URL = https://somebucket.s3.amazonaws.com/media/
Inside the urls.py I set the code as follows:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_URL}));
My intention is that, when I point an image file from, lets say <img src="/media/image.jpg"/> it will be automatically pointed to http://somebucket.s3.amazonaws.com/media/image.jpg
How can I do that? I have tried many methods but it always returns a 404.
However if i try to access the file directly http://somebucket.s3.amazonaws.com/media/image.jpg it works.
Why would you want to do that? That defeats most of the purpose of having the external storage in the first place. It means that for every media request, it has to go through Django itself, to resolve the URL and generate the redirect to S3, with all the overhead that implies.
Instead, as sneawo suggests in the comments, you should simply set the img src attribute to point to the image via the S3 URL.
turned out that django automatically appends the media_url in front of the imagefield url.
i was under impression that i have to append the media_url, which caused me to look for a simpler solution.
Django Admin only shows relative paths
![django admin shows relative paths][1]
http://i.stack.imgur.com/aZGPy.png
But, tastypie gives me absolute path
http://i.stack.imgur.com/SEdaX.png
so i now don't have to worry about urls anymore...thanks again guys :)

Django admin shows full path to uploaded file, and link contains full path. How can I fix this?

I have a model which is defined like this:
class Attachment(models.Model):
file = models.FileField(upload_to=MEDIA_ROOT)
MEDIA_ROOT is defined using it's absolute path, and it's something like d:\django\my_proj\media . In the admin, the link to it appears like this: http://localhost:8000/media/d:/django/my_proj/media/file.txt . How can I fix this?
Use / instead of MEDIA_ROOT. From the docs, upload_to should be:
A local filesystem path that will be
appended to your MEDIA_ROOT setting to
determine the value of the url
attribute.
In other words, the path that you specify in upload_to will be appended to MEDIA_ROOT to form the upload directory, and appended to MEDIA_URL to form the URL to the file.
p.s. it might be worth specifying a subdirectory instead of / just so the uploaded files cannot overwrite your media files. For example, upload_to='uploads/'. To organise you uploads by year/month, you can even do upload_to='uploads/%Y/%m/'. See docs for more details.