Redirecting to included URLS from RedirectView - django

Say I have a URL setup like this in my Django 1.6 project:
urlpatterns = patterns('',
url(r'^some-path/', include('someapp.urls')),
url(r'^$', RedirectView.as_view(url='some-path/', permanent=False)),
)
…but I want to change "some-path" to "changed-path" instead, and in the process realize that I've got it hardcoded in an extra place: the RedirectView setup!
Is there a proper way to do something like this, reversing to an included bundle of URLs?
# NOT WORKING! Django ignores `name=` when using `include()`
urlpatterns = patterns('',
url(r'^changed-path/', include('someapp.urls'), name='foo'),
url(r'^$', RedirectView.as_view(pattern_name='foo', permanent=False)),
)

First of all, you have to go to your urls.py file from someapp and get the name of your base url.
Let's assume it's something like:
# someapp/urls.py
urlpatterns = patterns(
'someapp.views',
url(r'^$', 'your view', name='foo'),
...
)
And now in your main urls file, you can write everything like this:
# WORKING! Because Django likes namespaces
urlpatterns = patterns('',
url(r'^changed-path/', include('someapp.urls', namespace='bar')),
url(r'^$', RedirectView.as_view(pattern_name='bar:foo', permanent=False)),
)

Related

The pages aren't found for an app

Main url file:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
)
My app url file:
from django.conf.urls import patterns, url
from mainapp import views
urlpatterns = patterns('',
url(r'^$', views.index),
url(r'^url1$', views.url1),
url(r'^url2$', views.url2)
)
When I go to "/", it shows the views.index from my app fine. However, it says "Page not found (404)" for url1 and url2.
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^$
^admin/
The current URL, url1, didn't match any of these.
What's up with that?
You are matching '^$', which is an empty string, so your other urls are not called.
Try:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('myapp.urls'))
)
Note that I changed the ordering. This is so that myapp will never override other, more specific urls like admin.
r'^url1$'
^ means start here;
$ means end here.
so, it is an empty string. it does not make sense to put anything between like you did ^ ... $.
how about to try: r'^$', 'views.url1'),
?

Need a seconda pair of eyes on Django URL dispatcher

Wondering if I could get some help with a second pair of eyes. Started working Django about two months ago learning in my spare time. Long story short: My index and post view work fine. I recently created a contact html\template and view which looks like it goes right back to my index page? Here is my project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from .settings import MEDIA_ROOT
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^contact/', include('blog.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
)
Here is my app urls.py
from django.conf.urls import patterns, include, url
from blog import views
urlpatterns = patterns('blog.views',
url(r'^(?P<post_name>\w+)/$', views.post, name='post'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^$', views.index, name='index'),
)
If I change my contact view to come up as my default the view it comes up fine.
urlpatterns = patterns('blog.views',
url(r'^(?P<post_name>\w+)/$', views.post, name='post'),
url(r'^$', views.contact, name='contact'),
#url(r'^$', views.index, name='index'),
)
As soon as switch it back to the original state when I click on the contact link on my page it goes right to my index page. I have to be doing something wrong with the URL dispatcher part but I'm not sure what.
Your contact urlpattern is not matching the URL "/contact", it is matching "/blog/contact" and "/contact/contact". Since you've included the blogs urlpatterns under both those prefixes, the thing that matches "/contact" is the URL without anything after the prefix, ie the index.
You probably don't want to put the contact pattern into the included file at all: just match it directly in the base project file.
Please mind the order of the urls in your app's urls.py. Try to put your views.contact view before your views.post view. Keep in mind: Django chooses the first matching regular expression, which is your views.post view.
I ran into a similar problem and described the solution here.
Here your contact view has the url /contact/contact/ and index view has the url /contact/
So you have rewrite the project urls as
from django.conf.urls import patterns, include, url
from django.contrib import admin
from .settings import MEDIA_ROOT
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^', include('blog.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
)

Django language switch not working

I would like to translate URL prefix and also URL slug using django-modeltranslation where slug is saved inside database table. After switching the language i would like to stay on the same page and just change the language. I'm using form language switcher as described here:
http://docs.djangoproject.com/en/dev/topics/i18n/translation/#the-set-language-redirect-view
Problem is that the language is just switching on homepage. The other pages are just refreshed without language and URL change.
Is there any way how can i get current url in other language?
In root project urls.py i have following:
urlpatterns = patterns('',
# Examples:
(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^$', 'portfolio.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(_(r'^projects/'), include('projects.urls', namespace='projects')),
)
in app called projects i have urls :
urlpatterns = patterns('',
url(r'^$', all_projects, name='projects'),
url(r'^(?P<slug>[\w-]+)/$', project_detail, name='project_detail'),
)
If this is not a copy-paste-problem you're missing url function name in your main urls.py. Change line 3 of your provided code above to:
urlpatterns = patterns('',
...
# The following line need to be changed from
# (r'^i18n/', include('django.conf.urls.i18n')),
# to
url(r'^i18n/', include('django.conf.urls.i18n')),
...
)

Django application urls not working

In one application's urls.py I have:
urlpatterns = patterns('app.views',
url(r'^products/$', products, name="products"),
url(r'^$', index, name="index"),
)
In base project urls.py I have:
urlpatterns = patterns('',
(r'^$', include('app.urls')),
(r'^admin/', include(admin.site.urls)),
)
Why http://127.0.0.1:8000/ - works fine with app.views.index method
while http://127.0.0.1:8000/products/ - returns 404 error and is not defined in url routes?
Spent some time on it already and can't find solution, maybe there is something simple that I miss...
Your base urls should be:
urlpatterns = patterns('',
(r'^', include('app.urls')),
(r'^admin/', include(admin.site.urls)),
)
The '$' is only used for urls. If you look at the doc, it will tell you not use the '$' when using include().
urlpatterns = patterns('',
(r'^', include('app.urls')),
(r'^admin/', include(admin.site.urls)),
)
worked fine.
I was having the same issue while using path() in Django URLs.
The simple fix is you don't have to use the slash at the end of the path otherwise Django will take that URL as a complete URL and will not go to the next urls.py file
//this will not work
path('/', include('app.urls'), name='profile_page')
// but this will work
path('', include('app.urls'), name='profile_page')

Why Django automatically adds slash after url that ends with ".htm" and doesn't when url ends with ".html"?

I have a problem that Django automatically adds slash to urls that ends with ".htm"
Url like:
http://127.0.0.1:8080/js/tiny_mce/themes/advanced/link.htm
becomes:
http://127.0.0.1:8080/js/tiny_mce/themes/advanced/link.htm/
But if I rename "link.htm" to "link.html" then no problem happens.
Where could be the issues?
Thanks.
urls.py:
from django.conf.urls.defaults import *
from dtunes.views import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', home, name='home'),
url(r'^(?P<path>.*\.(htm|html|jpg|jpeg|css|gif|js|png))$', "django.views.static.serve", {
"document_root": settings.MEDIA_ROOT,
}, name="media"),
url(r'^img/tr.gif', track, name='track'),
(r'^admin/', include(admin.site.urls)),
url(r'^smscoin/ipn/', ipn, name='smscoin_ipn'),
url(r'^download-link/', get_download_link, name='get_download_link'),
url(r'^get/(?P<name>.*)/$', item_details, name="item_details"),
url(r'^getnow', item_details_paid, name="item_details_paid"),
url(r'^download/(?P<name>.*)/$', send_direct_file, name="send_direct_file"),
url(r'^(?P<name>.*)/$', plain_page, name="plain_page"),
)
Django has a setting, "APPEND_SLASH", which adds a slash to URLs that are not otherwise matched in the URLConf, but would be matched if a slash were added. So you probably have some regex pattern in your urls.py that is matching ".htm/".
It looks like you are using Django to serve static files? If so, you might make sure this is configured properly. During development, to keep things DRY, I usually use the following in my "urls.py" file to serve static media. This requires a properly configured MEDIA_ROOT and MEDIA_URL in settings.py:
# urls.py
from django.conf import settings
urlpatterns = patterns(
...
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^%s/(?P<path>.*)$' % settings.MEDIA_URL[1:-1],
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)