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'),
Related
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
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?
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'),
]
I have started learning Django, I'm not sure what the include() function means.
Here is mysite/urls.py. - project
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
Here is polls/urls.py. - app in project
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
From django document, include() function was described as follows.
Whenever Django encounters 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.
I'm not sure what is that point, what is remaining string.
In case of the example above, what is remining string, what is url strings which was chopped off?
For example, from this URL:
polls/5/results
the URL rule:
url(r'^polls/', include('polls.urls')),
chops off the polls/ part of URL and sends the remaining string after polls/, whatever it might be, for example (see here more):
5/results/
to urls from the poll app's urls.py, where it will then be mapped to a view based on the URL rules defined in this file
Whenever it will encounter any url with /polls then it will include all the urls of polls app.
Example:
If you type /polls/hey
Then as soon as it sees /polls it will go to polls urls file and later it will search for:
hey/ matching over there.
Lets say there is one more entry in your polls/urls.py like
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
here year is the query string parameter. so your url will look like
/polls/articles/2007 so in this case /polls/articles/ will matched up and 2007 will pass to year_archive method
In your example there is no chopped string, the URL comes back as simply polls/, but when you have another url such as '^new$' then that url is being chopped, merged with polls/ and it returns polls/new, hope this makes sense..
I am pretty new to Django and when I laid my site out I did it in the following way:
The project is a "portal" of sorts,
App 1 "home" is the app that houses the homepage, login, registration
and other "site-wide" functionality
App 2 "inventory" is a basic asset inventory
App 3 "dashboard" is a set of status dashboards based on assets in the inventory
Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('home.urls'), name='home'),
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
)
My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.
Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404
File: /home/urls.py
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^test/$', views.index, name='home_test'),
url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
url(r'^$', views.index, name='home_index'),
)
At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?
Thanks in advance!
EDIT
Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.
Here is my new portal/urls.py file for reference
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
url(r'^', include('home.urls'), name='home'),
)
The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.
Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).
So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.
Django looks in urls like this:
Get list of root urls - portal/urls
Find first, which matches to current url
If it is include, go to included urls, cutting off matched part
Your problem is here
url(r'^$', include('home.urls'), name='home'),
I mean, here
'^$'
You want url to match "start and then end of url". It works good with root(dunno.com/), but not with others, because url will contain something more...
So, just remove $ and you are fine
url(r'^', include('home.urls'), name='home'),
you never need to add regex($) at project/urls.py