Django Текущий путь, newapp/1, не соответствует ни одному из этих путей
При попытке получить доступ к 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.
следуя руководству по опросам в официальном документе
( https://docs.djangoproject.com/en/4.0/intro/tutorial01/), приложение polls работает нормально. и индекс newapp также работает.
но когда я пытаюсь расширить приложение, создавая новые страницы для него (а именно testform/, thanks/, 1/), я получаю 404 в ответ.
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
]
Что меня озадачило, так это то, что фреймворк понимает, что у меня есть представление и ссылки на testform, thanks и 1. но они не могут быть доступны вручную через браузер?
обновление:
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"))
]
Я не думаю, что это проблема с проектом urls.py, поскольку я могу получить доступ к индексной странице нового приложения.
Измените файл вашего проекта urls.py на следующий:
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"))
]
и app 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),
]
Теперь вы можете проверить https://localhost:8000/newapp/1/
Измените файл вашего проекта urls.py на следующий:
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"))
]
и app 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),
]
Теперь вы можете проверить https://localhost:8000/newapp/1/