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')
Related
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'),
?
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}),
)
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)),
)
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')),
...
)
When I try to load urls described in different parts of urlpatterns, it loads the same view. Obviously, there is an error in urlpatterns I use but I can't find it.
Here is the first urls.py file:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', home),
url(r'^comments/', include('django.contrib.comments.urls')),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}),
url(r'^episodes/', include('episodes.urls')),
url(r'^news/', news),
url(r'', include('zinnia.urls')),))
And here is urlpatterns in episodes.urls:
urlpatterns = patterns('',
(r'$', seasons_list),
(r'(?P<season>\d{1})/$', episodes_by_season),
(r'(\d{1})/(\d{1})/$', episode),
)
Everytime I try to load url like "/episodes/1" or "/episodes/1/2", django uses only seasons_list view.
That's because all URLs have an end-of-string, hence they always match the first pattern. Add beginning anchors to all of them, since included URLconfs only get the part after the match.
Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
urlpatterns = patterns('',
(r'^$', seasons_list),
(r'^(?P<season>\d{1})/$', episodes_by_season),
(r'^(\d{1})/(\d{1})/$', episode),
)