Quick question and I don't know why this isn't easier to find but how do I switch between the home page index to another app's index page. My current setup:
kkquit/index.html - homepage
pack/index.html - page I want to link
What is the proper way to label a button to go from my index home page to another apps index page?
You can declare an app_name in the urls.py for both apps... For example:
Within the kkquit app urls.py file you'll have...
from django.urls import path
from . import views
app_name = 'kkquit' # declaring the app_name here
urlpatterns = [
path('', views.homepage, name='home'),
]
And within the pack app urls.py file you'll have...
from django.urls import path
from . import views
app_name = 'pack' # declaring the app_name here
urlpatterns = [
path('', views.homepage, name='home'),
]
Now in your project's folder... You'll have these be included within your project's urls.py file like:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('kkquit.urls', namespace="kkquit")), # Adding the value of this app app_name from the url.py kkquit as the namespace
path('pack/', include('pack.urls', namespace="pack")), # Adding the value of this app app_name from the url.py pack as the namespace
]
Now with that been set up you can navigate between the two apps from your front end.
You have two buttons or nav links, for example:
kkquit index page
pack index page
Related
i have a basic django app and want to exclude a route from matching other routes with similar structure
below is my urls.py
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns = [
path('', views.index, name = 'index'),
path('create', views.create, name = 'create'),
path('<str:handle>', views.show, name = 'blogs'),
]
i want path('create'), views.create, name = 'create') to go to the create blog page, and path('<str:handle>', views.show, name = 'blogs'), to go to the details page for a blog.
i get a 404 blog not found error when i navigate to http://localhost:8000/blogs/create
how do i exclude the create from matching the <str:handle> for a blog detail page?
Edit
the url.py for the main app urls
urlpatterns = [
path('blogs/', include(('blogs.urls', 'blogs'), namespace = 'blogs')),
path('admin/', admin.site.urls),
]
Option 1
Did you put your app in installed app in project settings.py.
Option 2.
You need to mention the blogs/urls.py files in project urls.py files.
I am new to Django and I am trying to build an image classifier app, I just tried building the app but I am getting this error, how may I resolve it?
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/imageupload
Using the URLconf defined in imageclassifier.urls, Django tried these URL patterns, in this order:
imageupload ^$ [name='home']
admin/
The current path, imageupload, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
These are all changes I made in my app:
This is the views.py file in imgUpload app
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html')
This is the urls.py file in imageUpload app
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path('^$', views.home, name ='home'),
]
And this is the urls.py in the Imageclassifier folder:
"""imageclassifier URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('imageupload', include('imgUpload.urls')),
path('admin/', admin.site.urls),
]
Also, I have added a home.html file in the templates folder. How may I fix this error?
In the latest django path methods, You do not need to use ^$ in your path. Simply remove that ^$ from your path and it will work fine.
I have 2 django app. user and management. I have added dashboard/ in main app urls which redirects to user app urls. For this we need to login. All urls in user app is: dashboard/ and for login dashboard/login
but it shows error in /login.
ERROR:
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/login
Main app urls.py
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('dashboard/', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
User app urls.py
from django.urls import path, include
from nh_user import views as users_views
from management import urls as management_urls
urlpatterns = [
path('login', users_views.user_login, name='user-login'),
path('logout', users_views.user_logout, name='user-logout'),
path('dashboard/', include(management_urls)),
path('', users_views.index, name='user-index'),
]
You specified dashboard/ as the route for all the urls in your users app. Django won't look for these urls unless you include '/dashboard/' in your address bar.
Currently the working path is http://localhost:8000/dashboard/login.
If you want to change your login path to http://localhost:8000/login you need to remove 'dashboard/' as the route in your project urls.py.
from user import urls as user_urls
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(user_urls))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
All the routes for the urls in your users app will now be at directly at http://localhost:8000/route-name without anything preceding them.
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 am trying to map the base URL to my "Learning Logs's" home page. here is the following code I have in my main urls.py file:
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path(r' ', include('learning_logs.urls', namespace='learning_logs')),
]
I save the file and look at the terminal to see if there are any issues and it spits out the following error: ModuleNotFoundError: No module named 'learning_logs.urls'
I am no to python/django and following a book called the python crash course. not sure what I am doing wrong please help!
Best solution is check author's updates:
http://ehmatthes.github.io/pcc/chapter_18/README.html#updates
updates for urls.py
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('learning_logs.urls')),
]
Updates for learning_logs/urls.py
"""Defines url patterns for learning_logs."""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
# Home page.
path('', views.index, name='index'),
]