Url rewrite for django ajax site in dev server - django

I'm developing an Ajax site where routing is controlled with JavaScript.
I want every request to be sent to the IndexView except those declared in the urls.py file.
Here is my urls.py configuration:
urlpatterns = patterns('',
url(r'^api/', include('api.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', IndexView.as_view()),
)
If I change the regex to url(r'^', IndexView.as_view()) it works but files in MEDIA_ROOT won't be displayed.
I know in production I can config the server to serve the static files the way I want but I want it to work with the dev server.

Silly solution. Adding url(r'^', IndexView.as_view()) after,
if settings.DEBUG:
urlpatterns += patterns('',
(r'%s(?P<path>.*)' % settings.MEDIA_URL[1:], 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
did the job.
But I'm open to better ways for managing routing with this kind of ajax applications.

Related

Django's APPEND_SLASH setting does not work with static, why?

Django's 4.1.1 APPEND_SLASH setting automatically appends a slash / until I add static roots, i.e. urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT).
For example, this works with http://127.0.0.1:8000/admin and http://127.0.0.1:8000/admin/
urlpatterns = [
path('', home),
path('admin/', admin.site.urls),
]
However, adding my static roots and the setting no longer takes effect:
urlpatterns = [
path('', home),
path('admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Why? Is this a bug? How is urlpatterns += static etc impacting Django's setting?
When you append static to your URLconf, it takes all URLs that do not match existing URL patterns and attempts to serve them through the view django.views.static.serve, which does not respect settings.APPEND_SLASH.
This should only be a problem in development, as you'll be serving static files through nginx/apache/whatever in production, but this is a good opportunity for you to sanitize all of your URLs in templates, etc. to include trailing slashes (hint: use the {% url %} template tag). You won't be able to do much about users typing URLs directly without the trailing slash, but again, this is only in development.
You can also use Whitenoise in development to get around this (http://whitenoise.evans.io/en/stable/django.html).

django static files with a different path

I'm trying to make the local development work with everything under a /abc.
For example, instead of http://localhost:8080 I want everything to be at something like http://localhost:8080/abcd
I was able to achieve this by updating the url.py with the following:
urlpatterns = patterns('',
url(r'^abcd/admin/', include(admin.site.urls), name='admin'),
url(r'^abcd/search/?$', views.search, name='search'),
url(r'^abcd/$', views.index, name='root')
)
# Force Django to serve static assets, if this is not the production
if settings.PRODUCTION is False:
urlpatterns += patterns('', (
r'^abcd/static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT
}
))
Now I can view the pages with http://localhost:8080/abcd but the rendering does not show up correctly because of the static files. I need the static files to be served at http://localhost:8080/abcd/static/ and for it to work with all the pages.
Is there a simple way to make it work? I tried doing the above and it doesn't seem to work. I'm super new to django so I don't fully understand how I can achieve what I'm trying to do. Any help from experts would be much appreciated.
It should work.
urlpatterns = patterns('',
url(r'^abcd/', views.index, name='root')
url(r'^abcd/admin/', include(admin.site.urls), name='admin'),
url(r'^abcd/search/?$', views.search, name='search')
)
# Force Django to serve static assets, if this is not the production
if settings.PRODUCTION is False:
urlpatterns += patterns('', (
r'^abcd/static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT
}
))
change settings.py
STATIC_URL = '/abcd/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

How to Internally redirect from home page to Catalogue page in Django-oscar?

I'm using Django-oscar for my project. After Django-oscar config, I can see localhost:8000 empty, Instead, I want to redirect to localhost:8000/Catalogue Page
urls.py
urlpatterns = [
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', admin.site.urls),
url(r'', application.urls),
]
I expected output is. when i run localhost:8000 it should take to localhost:8000/catalogue

Django only matches urls with an app prefix/identifier

I'm having a problem with django's URL system. I'm using Django 3. I have the following in my project's 'urls.py'
urlpatterns = patterns('',
url(r'^$', include('siteadmin.urls')),
)
and this in the 'urls.py' of a django app in the project, called 'siteadmin':
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
#...trimmed
)
With this setup, the following happens:
http://localhost/ => works perfectly, renders the 'home' view
http://localhost/register/ => breaks. (gives 404 and django's standard "The current URL, register, didn't match any of these.")
However, when I change the project's 'urls.py' to include the following:
urlpatterns = patterns('',
url(r'^app/', include('siteadmin.urls')),
)
And include /app/ in 1. and 2., both urls work perfectly. That is to say:
localhost/app/ => works perfectly
localhost/app/register => works perfectly.
What am I missing? Why does #2 break in the first case, but not the second?
Remove the dollar sign from the regex in the project urls.py:
urlpatterns = patterns('',
url(r'^', include('siteadmin.urls')),
)

how to include urlpatterns in django?

In my project I have one app which have its own urls.py like this
urlpatterns = patterns('',
(r'^(?P<language>\w+)/$', 'MainSite.views.home_page'),)
(above file is in my application )
I am trying include this file in main(project's) urls.py
like this :
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG :
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
but after this I can able to call the MainSite's (app's) view but my admin url is not working
I tried
urlpatterns = patterns('',
(r'^$', include('myproject.MainSite.urls')),
url(r'^admin/', include(admin.site.urls)),
)
but after this this makes admin work but my app's view won't get called,
how do I solve this.
You're including your views at the root level. Since it comes before the urlpattern for the admin, the first urlpattern catches everything, so nothing is ever passed to the admin views.
The simplest fix is to simply reverse the order:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'', include('myproject.MainSite.urls')),
)
Then, your views will only catch anything the admin doesn't.