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.
Related
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
I started make a website for my school project, i use Django Frameworks
i want to make my homepage website, i already have html file, then what should i do to make Django read my homepage
enter image description here
this is my urls.py on main folder
urlpatterns = [
path('admin/', admin.site.urls),
path('katalog/', include('katalog.urls')),
path('', ) #what should i write here?
]
django doc: https://docs.djangoproject.com/en/3.0/topics/http/urls/#example, it offers the below example
from . import views
urlpatterns = [
path('articles/2003/', views.special_case_2003),
You have to create in your apps views.py file a FBV or CBV, the easiest is a FBV like below:
def someview(request):
context={}#if you want to add objects to the front end
return render(request, 'yourhtmlfile', context)
and then import it in your urls.py:
from . import views
urlpatterns = [
path('home/', views.someview),
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 new to Django and I am trying to create sample project in it. Please help me in resolving this error.
My Main URLs.py file
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^t20_predict/', include('PredictMatch.urls')),
]
My Apps URLs.py file
app_name = 'PredictMatch'
urlpatterns = [
# t20_predict/T20
url(r'^T20/$', views.t20index, name='t20index'),
]
becuase
url(r'^T20/$', views.t20index, name='t20index')
you are not expecting any id ,the url is just t20_predict/T20/ -> see here there is no id
if you want the url to receive id as well the url pattern should be
url(r'^T20/(?P<id>[0-9]+)/$', views.t20index, name='t20index')
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