I am learning Django. I build an app in which I am setting up the URL patterns but in all cases it's displaying the data for the index page.
appTwo urls.py File :
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.users, name='users'),
url(r'^$', views.help, name='help'),
]
MyProject urls.py file :
urlpatterns = [
url(r'^$', views.index, name='index'),
url('admin/', admin.site.urls),
url(r'^users/', include('appTwo.urls')),
url(r'^help/', include('appTwo.urls')),
]
If I call /users or /help, the browser display the data for index file only.
Is there something with a regex that I am doing wrong?
You're setting your urlpatterns in a wrong way. First of all, in you project's urls.py file you are saying that if a request is sent to /users or /help, then Django should look into your appTwo.urls. When Django gets there, it finds that the urlpatters are set so that everything that's empty after any of the aforementioned urls must be handled by the views.index, views.users and views.help. But as views.index is the first one in the list, then all the request end up being handled by that view.
By the way, if you're using Django >= 2.0 you no longer need to use the url() function, but the path() one instead, for which you can declare the paths as simple strings rather than regular expressions.
You should have something as follows:
MyProject/urls.py
from django.urls import path
urlpatterns = [
path('', include('appTwo.urls')),
path('admin/', admin.site.urls)
]
MyApp/urls.py
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('users/', views.users, name='users'),
path('help/', views.help, name='help'),
]
when django reaches here (in your apps url) while resolving the urls, see all three urls have empty regex as first param (so the first one will be selected), And as you see its index then you always show index page. You need to differentiate the urls. Something like this.
urlpatterns = [
url(r'^users/$', views.users, name='users'),
url(r'^help/$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
I think you also cant write url index with empty regex at top of other urls, otherwise it will always be selected.
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
I'm trying to set my django homepage, linked to the root of my website: i.e. http://127.0.0.1:8000/index/ but instead, I keep getting errors where Django is searching for myapp as the homepage: http://127.0.0.1:8000/myapp/. I would just like to land on a homepage with "index" in the url instead of "myapp/"
The error is as follows:
Using the URLconf defined in Main.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
[name='myapp_index']
publications/ [name='publications']
^static\/(?P<path>.*)$
The current path, myapp/, didn't match any of these.
Views.py
def index(request):
return render(request, 'index.html')
Main/urls
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
myapp/urls
from django.urls import path
from logbook import views
urlpatterns = [
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
path('publications/', views.publications, name='publications'),
]
Now, if I change the path in Main/urls.py to path('myapp/', include('myapp.urls')), I land on the appropriate homepage, except, I would prefer that there is "index/" listed in the URL instead of "myapp/".
I may be missing something very simple here. This is what I think is my MWE, happy to add more.
I was having the same problem and I was cracking my head finding a solution.
The solution was to erase browser's cache and history
I realized that because when I tried with incognito mode worked perfectly!
This should be in the main urls.py
path('', views.index, name='index')
instead of my_app/urls.py
path('myapp/', include('myapp.urls'))
path('index/', include('myapp.urls'))
That might get the desired result.
Did you try following:
Main/urls
...
path(r'^', include('myapp.urls')),
...
myapp/urls
...
path(r'^$', views.index, name='index'),
# path('', views.myapp_index, name='myapp_index'), ## REMOVE THIS ROW ##
...
first you must choose one of below lines in myapp.urls and delete other
you can not set two views for one url patterns
path('', views.index, name='index'),
path('', views.myapp_index, name='myapp_index'),
you have two solution for get index/ as main url
1.change Main.urls like below
path('', include('myapp.urls')),
to
path('index/', include('myapp.urls')),
2.change myapp.urls like below
path('', views.index, name='index'),
to
path('index/', views.index, name='index'),
you actually changed name of urls that using in python codes not path of urls
edit
Do you mean index/ like the following?
www.example.com/
so u must do that as above but delete "index/"
so u cant do it as below :
change Main.urls like below
path('', include('myapp.urls')),
to
path('^$', include('myapp.urls')),
or
change myapp.urls like below
path('', views.index, name='index'),
to
path('^$', views.index, name='index'),
This is what worked for me (Django2) - pointing to index action in 'static_pages' app's views.py as home page:
main urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('static_pages.urls')), #included urls from static_pages app
]
static_pages/urls.py:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
]
static_pages/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("First Django2 Message!")
Note that 'static_pages' app is added to INSTALLED_APPS in settings.py.
Now, calling http://127.0.0.1:8000/ I got: "First Django2 Message!".
I am very new to Django and I have created two apps in my project one is login & server_status. I have used the namespace method to access my login and server_status across both apps. It is working fine when I tried to access index but when I tried to access dashboard in server_status it is not working.
Reverse for 'server_status' not found. 'server_status' is not a valid view function or pattern name
# server_status URL
from django.urls import path
from . import views
urlpatterns = [
path('', include('server_status.urls', namespace='server_status')),
path('', include('login.urls', namespace='login')),
path('admin/', admin.site.urls),
]
# login app URL
app_name = 'login'
urlpatterns = [
path('login', views.index, name='index')
]
# project URL
from django.contrib import admin
from django.urls import include, path
app_name = 'server_status'
urlpatterns = [
path('', views.index, name='index'),
path('dashboard/', views.dashboard, name='dashboard'),
path('server_status/', views.server_status, name='server_status'),
path('request_access/', views.request_access, name='request_access'),
]
In server_status/templates/server_status/index.html
Login
I know this is very simple one, but it makes me very complicated.
In the posted code, you define urlpatterns twice, so the first set of urlpatterns is overridden by the second. Since server_status is defined in the first set, it is not present in memory, since you obliterate it in the second definition. index is the only pattern that survives. I think what you meant to do in the second stanza was to add to the urlpatterns with:
urlpatterns += [...]
To declare namespace you should do something like this:
# project URL
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('server_status.urls', namespace='server_status')),
path('', include('login.urls', namespace='login')),
path('admin/', admin.site.urls),
]
then you can use
Login
or
Server status index
I believe this is a simple question but I am having a hard time figuring out why this is not working.
I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('chart.urls')),
]
and it worked fine.
I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:
urlpatterns = [
path('admin/', admin.site.urls),
path('sales/', include('sales.urls')),
path('', include('chart.urls')),
]
and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/sales/
Raised by: chart.views.index
which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.
I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.
chart.urls:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('dashboard/', views.chart, name='dashboard'),
path('', views.index, name='index', kwargs={'pagename': ''}),
path('<str:pagename>/', views.index, name='index'),
]
sales.urls:
from django.urls import path
from . import views
urlpatterns = [
path('sales/', views.sales, name='Sales Dashboard'),
]
I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.
This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/
Reply to an OLD question but may be useful for others...
from django.urls import path, include
dont for get to import include
Do this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('chart.urls')),
url(r'^sales/', include('sales.urls')),
]
you are not doing it properly since you are not telling your app where go once it is loaded .this r'^' to go directly to chart.url url(r'^', include('chart.urls')),
I was wondering, in the url.py-files, why are parentheses not used when calling methods from the view.py-files?
For example, why does the following code not have a parenthesis at the end of views.index?
urlpatterns = [
url(r'^$', views.index, name='index'),
]