So I am new to Django, and I am learning it using an existing app in my company. When I look at the urls.py I see the following:
path('', include('django.contrib.auth.urls')),
path('', include(('apps.resource.urls', 'resource'), namespace='resource')),
path('', include(('apps.lib.urls', 'lib'), namespace='lib')),
So my question is why are there multiple mappings for '' ?
I thought it was one pattern and then a url mapping to resolve that pattern. Can anyone help me understand what is happening here?
Related
I have three apps in the project which have different templates with different URLs but one I go the someone app template page then the links on this page are showing to other app URL whereas I have different apps with different URLs name
When I changed the name of templates files of every app then working fine.
Please give me a solution what is the problem getting with the same name file in a different app.
Thanks
Admin App
urlpatterns = [
path('login', views.login, name='admin_login'),
path('register', views.register, name='admin_register'),
path('logout', views.logout, name='admin_logout'),
path('dashboard', views.dashboard, name='admin_dashboard')
]
templates
----pages
------login.html
------register.html
Customer App
urlpatterns = [
path('login', views.login, name='customer_login'),
path('register', views.register, name='customer_register'),
path('logout', views.logout, name='customer_logout'),
path('dashboard', views.dashboard, name='customer_dashboard')
]
templates
----pages
------login.html
------register.html
When I changed to
templates
----pages
------customer_login.html
------customer_register.html
Then working but I can not find an issue why is giving this type of error whereas I have a different app with different vies, templates, URLs, and pathname where I'm redirecting.
Thanks in advance
This is due to the order Django scans the url patterns to find a match and trigger the matched view.
If you really want to keep this design you will have to do it like so:
adminapp/templates
-----adminapp # add this
--------pages
---------login.html
etc.
so within your template dirs of each app add another dir level called after the app's name. That way you ensure that Django will pick the correct template.
I am trying to attach hobbieswithCSS.html file to my website, while using Django. I am a beginner when it comes to Django, so I have naturally came across some problems (in the title) like this.
I have this anchor tag on my homepage -
My Hobbies
I have this view in my views.py file -
def hobbieswithCSS(request):
return render(request,'basic_app/hobbieswithCSS.html')
I think, that the main problem will be with the urlpatterns in urls.py file, because I am not sure how to set it up. These are my urlpatterns -
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
url(r'^$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
Could anybody please tell me, how could I change the code in order to make that hobbieswithCSS.html file display, when I am trying to run it on my server?
It must be:
My Hobbies
Without basic_app.
Also you have the same path for index and hobbies . You have to change your url path:
urlpatterns = [
url(r'^$', views.index, name='index'),
...
url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
in your template update to
My Hobbies
I am having trouble understanding the following warning. I have a namespace called "v1" and I am using these namespaces to determine versioning in my API (using django rest framework). So, I have paths like these:
/v1/accounts/me
/v1/listings
Here is the URLs configuration (project/urls.py):
urlpatterns = [
path('admin/', admin.site.urls),
path('v1/accounts/', include('accounts.urls', namespace='v1')),
path('v1/listings/', include('listings.urls', namespace='v1'))
]
accounts/urls.py
app_name = 'accounts'
urlpatterns = [
url(r'^token/$', views.obtain_auth_token, name='obtain_token'),
url(r'^me/$', my_account, name='my_account'),
]
listings/urls.py
app_name = 'listings'
urlpatterns = [
path('', recent_listings, name='recent_listings')
]
Everything works as expected. All urls are dispatched. Versioning works. However, I keep getting the following error:
?: (urls.W005) URL namespace 'v1' isn't unique. You may not be able to reverse all URLs in this namespace
I know it is a warning and I might be able to suppress it; however, I want to understand why this is happening. Based on my URLconf and this warning, it seems like there cannot be multiple namespaced paths as "siblings." They need to be children of one namespaced path (e.g "v1"). If my understanding is correct, how am I supposed to create this URL configuration.
Basically what happens is that, namespace plays a significant role on reverse finding the url. For example:
In your example reverse('v1:obtain_token') will return /v1/accounts/token/. Lets say you have two urls with same name in accounts and listings, then you might not be able to find accounts url in reverse query. That is why the warning is for. Better if you use different namespaces for each include. In you case, it should be:
path('v1/accounts/', include('accounts.urls', namespace='accounts')),
path('v1/listings/', include('listings.urls', namespace='listings'))
Please read the documentations for more details.
Update
you can do the versioning like this:
path('accounts/', include('accounts.urls', namespace='accounts')), # accounts url
inside accounts app:
path('v1/token/', views.obtain_auth_token, name='obtain_token_v1'),
path('v2/token/', views.obtain_auth_token2, name='obtain_token_v2'),
...
Hello thank you very much for reading my question post.
I have different url path patterns in urlpatterns,
but Django URL dispatcher(re-path) calls the same view( views.selected_verb)
for the different URL expressed by Regular expression.
These urls call the same view(views.selected_verb)
http://127.0.0.1:8000/arabic/verbs/%D9%83%D8%A7%D9%86/
http://127.0.0.1:8000/arabic/verbs/%D9%83%D8%A7%D9%86/quiz/
Would love to know how to fix it(calls different views)
here is urlpatterns
urlpatterns = [
path('', views.index, name='index'),
path('verbs', views.verbs, name='verbs'),
re_path(r'^verbs/(?P<verb>.+)/$', views.selected_verb, name='selected_verb'),
re_path(r'^verbs/(?P<verb>.+)/quiz/$', views.quiz, name='quiz'),
]
Thank you very much again!
I think the issue is that .+ will match with anything, which includes %D9%83%D8%A7%D9%86/quiz/. Maybe you could try telling it something more explicit, like [A-Z0-9%]+. When the q character comes along in quiz, it will fail matching and then go to the next url pattern which should be the one you want.
So I think it should all look like this:
urlpatterns = [
path('', views.index, name='index'),
path('verbs', views.verbs, name='verbs'),
re_path(r'^verbs/(?P<verb>[A-Z0-9%]+)/quiz/$', views.quiz, name='quiz'),
re_path(r'^verbs/(?P<verb>[A-Z0-9%]+)/$', views.selected_verb, name='selected_verb'),
]
Website has a homepage in addition to several data-driven apps. No problem. But as I'm trying to add in other non-data-driven pages (about, mission statement, etc) I'm having trouble with by url directs.
settings.py url patterns includes:
url(r'^$', include('home.urls')),
url(r'^mission/$', include('home.urls')),
home/urls.py includes:
url(r'^$', views.index, name='index'),
url(r'^mission/$', views.mission, name='mission'),
Directing the browser to the homepage loads the index view as it should, but directing the browser to /mission/ also loads the index view.
I realize I'm probably missing something small (and fundamental) here, but I've read over the docs for the hundredth time and read about a lot of other people's questions about urlpatterns, but I just can't figure out what's going on. The include() statement in settings.py doesn't seem to be the problem. Since the home index view loads it's obviously being directed to home/urls.py, and that file is so simple that I just can't see what the problem would be.
Could someone please educate me so I can move on to my next Django brick to the face? I appreciate it.
SOLVED - Thank you Sohan Jain
Actual issue was use of r'^$' in settings URLPATTERNS rather than r''. Use of the second include() statement was attempt to work around actual issue.
When you include urls from another directory, their path must start with the first parameter.
So when you say url(r'^$', include('home.urls')), that means: for each url in home.urls, make its path start with ^$, i.e nothing.
And when you say url(r'^mission/$', include('home.urls')), that means: for each url in home.urls, make its path start with 'mission'.
And urls are matched in order. So navigating to /mission/ matches url(r'^mission/$', include('home.urls')) and then url(r'^$', views.index, name='index'), which yields the index page.
Here's what you want:
settings.py
url(r'', include('home.urls'))
home/urls.py
url(r'^$', views.index, name='index'),
url(r'^mission/$', views.mission, name='mission'),