[英]Django url not finding a template using CreateView in Class-Based Views
我創建了一個頁面,用戶可以在其中添加新項目(在這種情況下為注釋),並且正在使用我最近開始學習的CBV。
這是我的表格
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('title', 'note', 'tags')
這是views.py中的視圖
class NoteCreate(CreateView):
model = Note
form_class = NoteForm
template_name = "add_note.html"
這是我在應用程序的urls.py中使用的網址
from django.conf.urls import patterns, url
from . import views
from madNotes.views import NoteCreate, NoteIndex,
urlpatterns = patterns(
'',
url(r'^notes/add/$', NoteCreate.as_view(), name="new_note"),
url(r'^$', NoteIndex.as_view()),
url(r'^(?P<slug>\S+)/$', views.NoteDetail.as_view(), name="entry_detail"),
)
注意:我在項目urls.py文件中使用了與主頁相同的url,即127.0.0.1:8000。
我看過一些教程,甚至是文檔 ,但似乎找不到我做錯的事情。 我是否還需要添加一個函數以將其保存在數據庫中,否則CBV會全部完成?
EDit:我得到的錯誤是
找不到頁面(404)請求方法:GET請求URL: http : //127.0.0.1 : 8000/notes/add/
這是項目的urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from MadNotez import settings
from registration.backends.default.views import RegistrationView
from madNotes.forms import ExRegistrationForm
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
url(r'accounts/register/$', RegistrationView.as_view(form_class = ExRegistrationForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', include(admin.site.urls)),
url('^markdown/', include('django_markdown.urls')),
url('^notes/', include('madNotes.urls')),
#url(r'^$', views.NoteCreate.as_view(), name="new note"), when I used it here it worked
)
您說這是app
的urls.py
,這意味着它包含在項目的urls.py
。
如您現在所顯示,所有應用程序的URI都位於notes
前綴下:
url('^notes/', include('madNotes.urls')),
因此,就目前情況而言,該頁面的正確URI是
http://127.0.0.1:8000/notes/notes/add/
為了進行一些清理,我建議將應用程序的urls.py
修改為
url(r'^add/$', NoteCreate.as_view(), name="new_note"),
這樣就可以到達該頁面
http://127.0.0.1:8000/notes/add/
這樣,所有應用程序的頁面/服務都可以在notes
前綴下使用,並具有與其操作一致的簡單名稱(即add
, delete
等)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.