This is my main urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include("shop.urls")),
]
I want that any url entered by user will redirect to shop.urls and find there
like if the user enters /index it will search index in shop.urls not in main urls.
My shop.urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('index', views.index),
]
You need to add / at the end of route so:
urlpatterns = [
path('', views.index),
path('index/', views.index),
]
Then enter the requested url as http://127.0.0.1:8000/index/
In main urls:
just give route name.
urlpatterns = [
path('shop/', include("shop.urls")),
]
And this is your shop urls:
urlpatterns = [
path('index/', views.index),
]
After changing above code.
You can navigate like this in your browser:
localhost:8000/shop/index/
You will redirect to index page.
Related
from django.contrib import admin
from django.urls import path, include
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('dashboard/', include(pages.urls)),
]
I'm trying to use include method under my urls.py file to direct the dashboad/ request to my app urls.py file, which is not working.
change this line :
path('dashboard/', include(pages.urls)),
to this :
path('dashboard/', include('pages.urls')),
see this link
Hi and welcome to StackOverflow!
The problem you are facing is that Django expects the include(...) method to be given a list of url patterns or the string of the module url's you want to be included at the path.
I would refactor your code like so:
from django.contrib import admin
from django.urls import path, include
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('dashboard/', include('pages.urls')),
]
I'm trying to reach the Django admin page but when I type in http://localhost:8000/admin/ it pops up with my web application instead.
For more context I have two url.py's in separate folders.
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.project_list, name='list'),
path('<slug:project_slug>/', views.project_detail, name='detail')
]
and...
from django.urls import path, include
urlpatterns = [
path('', include('budget.urls')),
path('admin/', admin.site.urls),
I'm not sure why http://localhost:8000/admin/ doesn't go to Django's admin page but if anyone has any suggestions that would be very much appreciated.
In your project level urls.py place admin url in top.
Like this
urlpatterns = [
path('admin/',admin.site.urls),
path('',include('budget.urls'))
]
Change urlpatterns order here:
from django.urls import path, include
urlpatterns = [
path('', include('budget.urls')),
path('admin/', admin.site.urls),
like this:
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('budget.urls')),
In your code pattern path('<slug:project_slug>/', views.project_detail, name='detail') resolve before path('admin',admin.site.urls),.
See Django documentation
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 have in my main urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('scoring.urls', namespace='scoring')),
]
and in my app urls:
urlpatterns = [
url(r'scoring/(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
url(r'^scoring/$', views.index, name='index'),
]
and in my template:
<li>Scoring</li>
but what {% url 'scoring:index' %}generates is localhost/instead of localhost/scoring. Why?
At first, you can add the ^scoring prefix in the main urls.py file, instead of writing it everywhere in your scoring urls:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^scoring/', include('scoring.urls', namespace='scoring')),
]
Then in your scoring urls.py make sure to add app_name:
app_name = 'scoring'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<query_id>[0-9]+)/$', views.get_table_data, name='table_api'),
]
(Note that I removed the scoring prefix in the url patterns.)
Now, as you've added app_name, the reversing in your template should work as expected.