Django served on specific URL - relative urls corrupted - python

I've deployed my Django (DRF API) project on DigitalOcean apps. The path "/" is used by my static site that uses this API so I set route for this component to "/api" which works correctly.
The problem:
I go to /api/admin/ and it redirects me to /admin/login but the django is served on /api url so this URL is invalid.
Do you know how to make this work?
Is there a way to tell django to use absolute URL everywhere?

urls.py
urlpatterns = [
...
path("api/admin/", admin.site.urls),
...
]

Related

Redirect from .com/ to just .com

I have a web page served by Django, and i want to make it to 301 redirect from
mysite.com/ to mysite.com without a slash on the end. Is it possible to do with Django?
Last version of Django, python 3.6.
path("", views.home, name="main_page")
Changing nginx config is not an option at my server
mysite.com/ and mysite.com are the same URL. You don't need to set up a redirect.
Your friend probably means that you shouldn't have the same content for mysite.com/foo and mysite.com/foo/.
In Django, you would usually achieve this by using the URL foo/, then the default setting APPEND_SLASH = True will redirect /foo to /foo/.

Django URL Config For Dev and Prod

My django app's urls are setup as follows for my development machine:
urls.py:
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^signin$', signin, name='signin'),
url(r'^signout$', signout, name='signout'),
url(r'^advisor/', include('apps.advisor.urls', namespace='advisor')),
]
This setup works fine while developing the app using Django's builtin server (e.g. http://127.0.0.1:8000). I have to deploy the app to a machine and the Django app is exposed by the following url:
http://ip address/appname/
I'd like to have my urls.py file configured so the regular expression in the url tests to see if the url request starts with "appname" or "" (empty string). This method would mean I don't have to create to urls.py files (dev and prod versions). Thanks for any help!
You do not have to do this at all. When your app is deployed via WSGI, the server should pass on the SCRIPT_NAME parameter which identifies the path it is mounted at. As long as you have consistently used {% url %} and reverse() throughout your app rather than hard-coding links, everything will just work.

What is the right way to setup urls.py for django-rest-framework + angular-route + static files?

I understand the concept of what I want to do (there is a rather nice question here: Django, REST and Angular Routes)
Django has a route for DRF, Admin, and needs at least one more, for the angular page.
But I'm not sure on the implementation details, particularly in where to put the frontend/static files, what to put in urls.py, and how to configure it for dev+production.
My url patterns currently look like this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Ideally, in production, I want serve the HTML without touching django - straight from nginx or similar (probably using AWS EB, but that is outside the scope of this question).
For Dev - I don't mind, as long as it works!
Skipping djangos templating system for angular would be great, rather than altering angulars operators. I don't intend to use django templates (except in djangos admin, and DRFs browsable views)
For development, I use something like this, at then end of the main urls.py file, to serve static files from various directories:
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
# this connects STATIC_URL to STATIC_ROOT
urlpatterns += staticfiles_urlpatterns()
# connect other paths to directories
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
urlpatterns += static('/app/',
document_root=join(settings.BASE_DIR, '../ng_app/'))
For production, you set the webserver to handle the requests. First the known routes like /admin/, /api/, /api-auth/ and files in /static/.
And then a catch-all route / that would always serve your index.html with the Angular app for any route. So that HTML5 routes in Angular will always load the app first, and the app then uses its own routes to build the correct page from API data.
The actual app files could be anywhere, even on a different server.

debug-toolbar spoiled my urls

I'm a newbie to Django and now I'm trying to make a simple webshop using it. Today I realized that I need some debug tool, so I've chosen django-debug-toolbar. I setup it just like it's explained here - How do I see the Django debug toolbar?. But when I set DEBUG to True I get a message -
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in urlconf, Django tried these URL patterns, in this order:
^__debug__/m/(.*)$
^__debug__/sql_select/$ [name='sql_select']
^__debug__/sql_explain/$ [name='sql_explain']
^__debug__/sql_profile/$ [name='sql_profile']
^__debug__/template_source/$ [name='template_source']
The current URL, , 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.
Here is my app's urls.py:
from django.conf.urls import patterns, url
urlpatterns = patterns('ecommerce.views',
url(r'^profile/$', 'profile'),
url(r'^login/$', 'login_user'),
url(r'^logout/$', 'logout_user'),
url(r'^registration/$', 'registration'),
)
As far as I can see now django looks for appropriate urls not in my app's urls.py, but somewhere else.

Why can't Django find my admin media files once I leave the built-in runserver?

When I was using the built-in simple server, everything is OK, the admin interface is beautiful:
python manage.py runserver
However, when I try to serve my application using a wsgi server with django.core.handlers.wsgi.WSGIHandler, Django seems to forget where the admin media files is, and the admin page is not styled at all:
gunicorn_django
How did this happen?
When I look into the source code of Django, I find out the reason.
Somewhere in the django.core.management.commands.runserver module, a WSGIHandler object is
wrapped inside an AdminMediaHandler.
According to the document, AdminMediaHandler is a
WSGI middleware that intercepts calls
to the admin media directory, as
defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
Use this ONLY LOCALLY, for development! This hasn't been tested
for
security and is not super efficient.
And that's why the admin media files can only be found automatically when I was using the test server.
Now I just go ahead and set up the admin media url mapping manually :)
Django by default doesn't serve the media files since it usually is better to serve these static files on another server (for performance etc.). So, when deploying your application you have to make sure you setup another server (or virtual server) which serves the media (including the admin media). You can find the admin media in django/contrib/admin/media. You should setup your MEDIA_URL and ADMIN_MEDIA_URL so that they point to the media files. See also http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files.
I've run into this problem too (because I do some development against gunicorn), and here's how to remove the admin-media magic and serve admin media like any other media through urls.py:
import os
import django
...
admin_media_url = settings.ADMIN_MEDIA_PREFIX.lstrip('/') + '(?P<path>.*)$'
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^' + admin_media_url , 'django.views.static.serve', {
'document_root': admin_media_path,
}, name='admin-media'),
...
)
Also: http://djangosnippets.org/snippets/2547/
And, of course, #include <production_disclaimer.h>.

Categories