找不到页面(404)notes.url

时间:2019-09-13 13:38:33

标签: python django

当我运行服务器时,会显示

找不到页面(404)

Django使用notes.urls中定义的URLconf,按以下顺序尝试了这些URL模式:

admin / notes /空路径与任何这些都不匹配

注意urls.py

from django.contrib import admin
from django.urls import include , path

urlpatterns = [
path('admin/',admin.site.urls),
path('notes/', include('notes_app.urls'))
]

notes_app urls.py

from django.conf.urls import url
from . import views

 urlpatterns = [
   url(r'^$' , views.all_notes , name='all_notes'),
  url(r'^(?P<id>[\d]+)$', views.detail , name='note_detail')

  ]

视图

  from django.shortcuts import render
  from django.http import HttpResponse
  from .models import Note
  # Create your views here.

  ## show all notes
  def all_notes(request):
   # return HttpResponse('<h1> Welcome in Django Abdulrahman </h1>' , {})
   all_notes = Note.objects.all()
   context = {
        'all_notes' : all_notes
    }

    return HttpResponse (request , 'all_notes.html' , context)

   ## show one note
   def detail(request , id):
   note - Note.objects.get(id=id)
   context = {
  'note' : Note
   }
[enter image description here][1]   return render(request,'note_detail.html' , context)

1 个答案:

答案 0 :(得分:0)

使用HttpResponse时,您不会传递模板:)

您传递了一个字符串,但是在这里您传递了一个html文件:

return HttpResponse(request , 'all_notes.html' , context)

不起作用,请详细了解here

所以像这样使用render

return render(request , 'all_notes.html' , context)

希望有帮助