0

I have a question about my django settings and nginx in order to display a download link receives in an email generated by a Django view.

1- Context

The emailing part works fine. I can receive this one. In this email I created a link which let to download a file stored in MEDIA folder.

My issue is about the url generated in the email which works with localhost, but not on my testing environment.

2- My code in my local environment

In order to build my download link, I pick up the protocol and domain through :

url = self.request.build_absolute_uri(reverse('home'))

Then, in my message, I created a link like this :

<a href="{{ url }}{% url '<my app>:export_download' token=token %}">Download link to your export file</a>

In local, it gives me :

http://localhost:8000//download_export/<my_file>/

As you can see, I have a double slashes in my generated url which makes an error. I solved this issue with :

url = request.build_absolute_uri('/').strip("/")

The urls.py file looks like this :

urlpatterns = [
    url(r'^home$', HomeView.as_view(), name='home'),
    ...
    url(r'^download_export/(?P<token>.*)/$', ExportDownloadView.as_view(), name='export_download'),
]

3- My code in my testing environment

In this environment, I'm using nginx as webserver. The application is available from : https://subdomain.domain.fr/dev3/<app_name>/home

The nginx.conf looks like :

location /dev3/<app_name> {
    include uwsgi_params;
    uwsgi_param SCRIPT_NAME /dev3/<app_name>;
    uwsgi_pass unix://var/run/uwsgi/<app_name>.sock;

When I access to the generated download like sent from my testing environment, it gives me an access to :

https://subdomain.domain.fr/download_export/<my_file> and not https://subdomain.domain.fr/dev3/<app_name>/download_export/<my_file>

It misses something ? How I can add this part /dev3/<app_name> ? By default, all other urls access to https://subdomain.domain.fr/dev3/<app_name>/something but not my generated link.

Thank you

EDIT :

In my uwsgi.ini file, I have this both lines :

mount = /dev3/%n=main.wsgi:application
manage-script-name=true

So it should work

ChocoBomb
  • 301
  • 4
  • 15

1 Answers1

0

You should check media_root in settings.py on your staging environment. Here's a great article on serving media files in Django that might help.

Sam
  • 1,952
  • 1
  • 18
  • 31
  • I don't understand your answer. Because my question is not about MEDIA, but about `nginx subpath` – ChocoBomb Feb 21 '19 at 13:44
  • You mentioned that the file you're trying to serve is in the MEDIA folder, which is why I linked the article; it deals with how to store and serve MEDIA files (e.g., files that have been uploaded by your user). Midway through, the article also links to this Stack Overflow post on how to deploy static and media files on nginx specifically: https://stackoverflow.com/questions/17391616/serving-static-files-with-nginx-gunicorn-django – Sam Feb 21 '19 at 14:31