Issue
I was greeted with this 404 when i tried to access http://192.168.68.106:8000/newapp/1
Using the URLconf defined in test_01.urls, Django tried these URL patterns, in this order:
1.admin/ **working fine
2.polls/ **working fine
3.newapp [name='index'] **working fine
4.newapp testform/ [name='testform'] **NOT working
5.newapp thanks/ **NOT working
6.newapp 1/ **Not working
The current path, newapp/1, didn’t match any of these.
following the poll tutorial on the official doc
( https://docs.djangoproject.com/en/4.0/intro/tutorial01/ ), the polls app works fine. and the newapp index also works.
but when I try to expand into the app by creating new pages to it ( namely testform/
, thanks/
, 1/
), I get a 404 in response.
views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from .forms import TestForm
class IndexView(generic.View):
template_name = "newapp/index.html"
def get(self, request, *args, **kwargs):
context = {
/mycontexts
}
return render(request, self.template_name, context)
class TestForm(generic.edit.FormView):
form = TestForm
template_name = "newapp/form_test.html"
success = "/thanks/"
def thanks(request):
return HttpResponse("thanks!")
def test1(request):
return HttpResponse("good")
urls.py
from . import views
app_name = "newapp"
urlpatterns = [
path("", views.IndexView.as_view(), name="index"),
path("testform/", views.TestForm.as_view(), name="testform"),
path("thanks/", views.thanks), #I tried to use a function instead of class based view, but failed to produce a success result
path("1", views.test1), #I didn't miss a backslash here, it was intentionally removed to see if it made a difference
]
what have be baffled is clearly the framework understand that I do have the view and urls to testform
, thanks
, and 1
. but they are unable to be accessed manually through browser?
update:
the project's urls.py
"""test_01 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/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 include, path
urlpatterns = [
path('admin/', admin.site.urls),
path("polls/", include("polls.urls")),
path("newapp", include("newapp.urls"))
]
I do not believe its an issue with the project's urls.py
as I can access the newapp's index page.
Solution
Change your project urls.py file to this:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path("polls/", include("polls.urls")),
path("newapp/", include("newapp.urls"))
]
and app urls.py to
from . import views
app_name = "newapp"
urlpatterns = [
path("", views.IndexView.as_view(), name="index"),
path("testform/", views.TestForm.as_view(), name="testform"),
path("thanks/", views.thanks), #I tried to use a function instead of class based view, but failed to produce a success result
path("1/", views.test1),
]
Now you can check https://localhost:8000/newapp/1/
Answered By - Aakash Yadav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.