My email link doesn't access to the right path - django

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 :
Download link to your export file
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

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.

Related

How to Configure django_plotly_dash to serve assets from custom url

I have an hybrid app with most pages being on vuejs but some legacy code pages are still being served through dash-django app.
previously it was wholly a dash app.
on the production environment this is being served through an nginx with dedicated static directory and a reverse proxy.
The app is being served on a subdomain.
So the url looks like :
abd.com/subdom/route_to_app_endpoints
where abd.com is set to serve nginx files from base directory
and abd.com/subdom is reverse proxy to serve files from port 8000 on which the django application is running.
The problem I'm facing is when the app loads the base url i tries to load the components and the layout from is hitting the root directory :
eg for
abd.com/.django_plotly_dash/app/FileUpload/_dash-layout and it
gives 404 not found. This is what it tries by default .
whereas if i request
abd.com/subdom/django_plotly_dash/app/FileUpload/_dash-layout my
browser gives a nice output ..
I tried setting :
PLOTLY_DASH = {
"requests_pathname_prefix" :"/subdom/"
}
in the settings.py but still it is unable to route properly.

Rewrite folder name from the URL in nginx

Hi I am new to nginx and trying to remove folder name from the URL.
Here is how my URL is accessible:
localhost/project_name/frontend/dashboard
localhost/project_name/frontend/feed
I want to remove the folder named 'frontend' from the URL. the folder contains all of my rendering templates. I have used following configuration but I get 404 when I enter the URL in browser after this configuration:
location /project_name/frontend {
try_files $uri $uri/frontend /project_name/ #extensionless-php;
}
I am not sure whether the configuration is correct, I have tried several configurations but none of them helps.
P.S: This is my just first day on nginx, so pardon if I miss something very obvious.
Thank your for your help.

Setting root path to rails app's root_path in nginx

I've configured a rails app on Digital Ocean (DO) using passenger, nginx and capistrano. Everything is working fine expect that I want to get the application's root_path page to be displayed whenever I go to the website. But that's not the case. I've to keep another page index.html in public directory and set it to root like:
xyz.conf file
root /home/user/apps/xyz/current/public;
I don't want this extra static page to be displayed. It just doesn't look good. Instead I want the application's root page which I specified in routes.rb file like root 'controller#method' to be displayed.
I've tried removing root line or restricting it to just /home/user/apps/xyz/current in .conf file but that just gives a 403 Forbidden error which should be the case.
I've thought a lot but can't think of anything. Anyone out there who had a similar problem and managed to figure it out?
Your nginx root is must be like:
root /home/rails/sample_tasks/public;
Note: path may vary but you need to set path till your app's public folder
then reload you Nginx
service nginx reload

Using lets encrypt without control over the root directory

I'm running a Django website and using lets encrypt for my SSL. Configuration of the framework is such that I can't allow access on: http://url.com/.xxxx
What I can allow free access to is:
http://url.com/static/.xxxx
My /static/ URL can accept and host any random files lets encrypt needs. Is there a way to have certbot support /static/ instead of just using / for the URL?
Thanks
EDIT
I've found a work around that is acceptable for me. Further digging, I found that /.well-known/ is always the base directory for SSL checking. That means we can add a static directory which will work nicely with certbot. Here's how, firstly add this into your apache config:
Alias /.well-known/ /var/www/XXXXX/website/static/.well-known/
<Directory /var/www/XXXXX/website/static/.well-known/>
Require all granted
</Directory>
Then add this into your settings.py file:
STATIC_ENCRYPT_URL = '/.well-known/'
STATIC_ENCRYPT_ROOT = '/var/www/XXXXX/website/static/'
Add this into your urls.py:
urlpatterns = [
...
] + static(settings.STATIC_ENCRYPT_URL, document_root=settings.STATIC_ENCRYPT_ROOT)
Reset your webserver. Now you have a special url /.well-known/ which will host any file certbot requires.
I'd still like a better answer than this.
In case other users come this way like I did from Google, here's how I improved this situation:
I was unsatisfied by my options when it came to creating ACME challenges for Let's Encrypt when running a Django application. So, I rolled my own solution and created a Django app! Basically, you can manage your ACME challenges as just another object, and the app will produce the proper end-point URL.
Simply pip install django-letsencrypt and follow the README to be on your way.

Why is my picture not displaying after upload with Django-Userena?

When I go to edit a user profile and upload a new profile pic it does not show up. Instead I see a broken link with the following error.
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/site_media/media/mugshots/4cfb402cad.jpg
Using the URLconf defined in dealr.urls, Django tried these URL patterns, in this order:
^$
^admin/
^accounts/
The current URL, site_media/media/mugshots/4cfb402cad.jpg, didn't match any of these.
What is strange about this is that inside my project folder I actually have /site_media/media/mugshots/ and the exact file that was uploaded (with the same filename), so I'm not sure why it is not displaying the picture. Do I need to do something to my URLS.PY to allow for the viewing of this file? That's the only thing I can think of.
It seems your server cannot find your files. Make sure you have configured your static and media roots in your settings.py (for manage.py runserver).
If you are using a production server you will need to tell it where those directories are.
As an example if you are working with nginx:
server {
[...]
location /static/ {
alias /path/to/my/staticfiles/;
}
}