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
Related
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/.
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.
I am new to Django and trying to make a project, but I am facing a simple problem. I am writing a path in Django 2 for root and it's not working, but for other things it works. Can anyone point out why it's not working.
What is working:
path(r'home/', home, name='home'),
This is not working:
path(r'^$', home, name='home'),
And just to be clear: I am not loading both the line together. I comment one line at a time, so no order issues.
Beginning with Django 2 a new way of defining URL routes is introduced. There are the functions path and re_path.
You seem to mix some things. Here is how it should look like when using path:
from django.urls import path
urlpatterns = [
path('', home, name='home'),
# or
path('home/', home, name='home'),
]
From this example you can obviously use only one path. I gave them both for illustrating.
As you notice the paths aren't raw strings and don't contain regular expressions like ^ or $.
For using regular expressions use re_path.
The index site on my Django homepage stopped working because of a problem that will take a very long time to fix. The site can't be down for that long so I am trying to change the index site so that if you go to the primary url you will atleast end up on the website.
What I have done is change the urls.py file in the primary application, where I simply replaced the line
url(r'^', include('news.urls', namespace='news')),
to
url(r'^', include('events.urls', namespace='events')),
in the urlpatterns list, where news is the faulty page and events is the page that I want to be shown. However, after pushing this to live nothing changed, and for some reason my local Django development server is not working.
Did I do anything wrong, or is there anything else I have to do as well?
Thanks.
In the events app make sure you have a URL such as
......
url(r'^$', views.EventsIndex.as_view(), name='index'),
......
Replacing news.urls with events.urls may cause problems, if there are views/templates that try to reverse news urls. It would be better to leave the include as it is, and add a new URL pattern above that for the index:
from events.views import home
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^', include('news.urls', namespace='news')),
...
]
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.