Been scratching my head quite a bit with this one, can't seem to figure out what I am doing wrong.
So, the problem is that I cannot seem to extract the path variable that I want from a urls.py inside an app to pass it to the function that should be called when the path is visited. This URL configuration where I am trying to get it is a nested one that is included when one visits events/, here is how it looks:
Root URL configuration:
urlpatterns = [
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^admin/', admin.site.urls),
url(r'^events/', include(event_urls))
]
And then the included URL conf from event_urls:
urlpatterns = [
url(r'alarm/<alarm>', alarm)
]
Should be pretty straight forward I imagine, but the above does not work, like at all! If I construct the URLs in this way, requesting to the path events/alarm/on will state that path does not exist. After this, I figured it could not match the URL, but I don't understand why or how to fix it. I thought (from the docs) that the part would match anything!
Now, I did some more investigating that proved to me that using a pattern for the might be the right way to go. Adding the regex way of capturing a path variable made the function actually trigger! So, the URL matcher was much happier after I did this:
urlpatterns = [
url(r'alarm/(?P<alarm>)', alarm)
]
Although, looking at alarm parameter in the alarm view function that gets called shows it as empty...
Found the solution in adding a proper regex to match what format I expect the <alarm> part to arrive in. I'm honestly still quite puzzled as to why simply putting <alarm> does not work, so to that I welcome an explanation! Anyway, here is how it looks right now in working condition:
urlpatterns = [
url(r'alarm/(?P<alarm>\w+)', alarm)
]
This matches a word of any length, I could make it more fancy but that is besides the point of this question. So, matching the content of <alarm> was what made it work.
Related
Currently learning about Auth0 and django.
https://github.com/auth0-blog/django-feed-auth0/blob/main/feed/feed/urls.py
I saw the URL pattern is like this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('feedapp.urls')),
path('', include('social_django.urls')),
]
From what i learned previously we should have 1 path('', views.xyz) as it will be redundant to have same url pointing to the different views unless we have put other input like int or str. But auth0 have same path with other views.
Not really understand why is it okay for it to be like this? Hope you guys can explain to me.
Thanks
As explained in Django documentation, Django will parse through each URL pattern, in order, and stops at the first one that matches the requested URL.
As long as the path in "feedapp.urls" and "social_django.urls" are unique, everything will be fine. If some path are not unique, the first one found will be used. In your case, those in "feedapp.urls".
To ensure uniqueness, we can use named path to remove ambiguity Including other URLconfs.
Hope the explanation will help you
I'm trying to make an example for a product page where people can put designs on their product's. I'm using django and try to make my "designer" page behave differently on the type of product selected.
I want to have it behave like www.mysite.com/designer/foo
where foo is the name of the product
I have gotten to make it work like www.mysite.com/designer?product=foo
but i like the simplicity of the first url more and i can't imagine that wouldn't be possible.
urlpatterns = [
path('', overview, name='overview'),
path('designer/', designer, name='designer'),
path('administration/', administration, name='administration'),
path('admin/', admin.site.urls),
]
my url patterns are like this, i've tried fiddling around with some examples using regex behind the "/" after designer. but i can't get it to work, the server keeps trowing
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/designer/exampleA
I'm afraid i don't know where to start looking and how to properly formulate my question as webdeveloping is quite new to me.
You can specify a pattern <str:designer> where we make use of the <str:…> path converter [Django-doc]:
urlpatterns = [
path('', overview, name='overview'),
path('designer/<str:designer>/', designer, name='designer'),
path('administration/', administration, name='administration'),
path('admin/', admin.site.urls),
]
Then your designer view function should take designer as parameter and handle this, so:
def designer(request, designer):
# …
here designer is a string, so this will be 'foo' if you visit /designer/foo/.
Hi thanks for looking into this.
I have been following Django's tutorial on URLs and got a bit confused/stuck on this part:
https://docs.djangoproject.com/en/1.4/intro/tutorial03/#decoupling-the-urlconfs
what I do not understand is if, say, on page mypage.com I provide only 2 possible URLs for mypage.com/polls and mypage.com/admin, what happens if the user goes to mypage.com? Obviously, I thought, the user will need to see some sort of 'welcome' page so I decided to add another URL to that urls.py:
urlpatterns = patterns('',
url(r'^/', 'myapp.views.welcome'), #when it's just mysite.com
url(r'^myapp/', include('myapp.urls')), #includes everything with mysite.com/myapp/...
url(r'^admin/', include(admin.site.urls)),
)
But then I get redirected to that welcome view from whichever page, whether i go to /myapp or not. So, I decided to create a views.py file outside myapps folder and put that welcome page there, and it seems to have worked, apart from that I get a 404.
I am so confused! Could you explain in lamers' terms please
Thanks,
blargie-bla
It should be
url(r'^$', 'myapp.views.welcome')
otherwise any URL will match the pattern. Django will call the view for the first pattern in urlpatterns that matches, so you need to be specific and include the end-of-the-line character ($) into the pattern.
I have been looking around the web for a solution but I cant seem to find a valid solution.
I am trying to override the default "index" page for a specific model. I know you can change the URL setting in the project's url.py folder like this :
url(r'^items/(?P<itemId>\d+)/$', 'items.views.itemDetail')
While the above URL mapping works fine and the itemDetail(request) method gets called correctly, something like this :
url(r'^admin/homepage/$', 'homepage.views.index'),
doesn't seem to work. I know this doesn't work because of this line:
url(r'^admin/', include(admin.site.urls))
Every attempt at changing the above line's pattern causes errors unless I go directly to admin/homepage. So the default admin page dies. I am simply trying to change the default index template to something different. Short of changing the URL completely from XXX/admin/homepage to something else, I have no idea how to solve this.
Any help will be greatly appreciated.
I think it should just work as long your custom url definition comes before the one for the actual admin in the urlconf (urls.py):
urlpatterns = patterns('',
...
url(r'^admin/homepage/$', 'homepage.views.index'),
...
url(r'^admin/', include(admin.site.urls)),
...
)
I've got a django urls.py file like so:
Base file:
urlpatterns = patterns('',
(r'^', include('sp.sp_app.urls')),
(r'^', include('sp.sp_api_activity.urls')),
(r'^', include('sp.sp_api_player.urls')),
(r'^', include('sp.sp_web.urls')),
)
In the sp.sp_web.urls file, I have the following:
from django.conf.urls.defaults import *
urlpatterns = patterns('superproof.superproof_web.views',
(r'^$','index'), #Shows your home page
(r'^challenge$','spcreatechallenge'),
(r'^player/`$','getlastactivity'),
(r'^yearlysummary/','yearlysummary'),
(r'^processchallenge$','processchallenge'),
(r'^activity/(\w{32})$','activitydetail'),
)
The yearlysummary url is loading sometimes. Other times, I get a 404 error. This happens with the exact same valid URL. I don't change anything in the URL, or in the code.
When I get the 404 error with debug turned on, my yearlysummary URL pattern isn't on the list.
Any ideas?
A couple things I noticed that could be an issue...
I notice you are not namespacing your included urls for each app. Not knowing what the other urls modules look like, I can assume its possible that you can have colliding urls. I Would recommend doing something like this:
urlpatterns = patterns('',
(r'', include('sp.sp_app.urls')),
(r'^activity/', include('sp.sp_api_activity.urls')),
(r'^player/', include('sp.sp_api_player.urls')),
(r'^web/', include('sp.sp_web.urls')),
)
Normally when you have url includes, they are for different apps, so you would want to namespace them to avoid two apps specifying the same url pattern.
Also, whats that back-tick in one of your urls? Did you mean to expect that?
(r'^player/`$','getlastactivity'),
jdi is spot on, on the analysis. I'll try to explain why exactly is there the issue, in your case.
You will notice that if you include (r'^', include('sp.sp_web.urls') as the first pattern in the main urls.py, your view will rightly load always.
When you have it as the last pattern, the reason it doesn't match the other times is that, that pattern is matching some other pattern in the earlier pattern, say [/w+]. The debug page, on a 404 of the url pattern, displays all the sub-url patterns of one of the patterns it matches. This is exactly why the pattern isn't on the urls displayed on the debug page.
Like jdi mentions it is a good practice to namespace the urls properly, so this doesn't occur. You can probably do it even without namespacing, but your regexes have to be proper, with the end character $ included, at least.