【发布时间】:2023-02-06 20:55:44
【问题描述】:
我是 Django 的新手。 我一直在基于 Mozilla 的模板工作:https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Tutorial_local_library_website
我创建了一个名为“debtSettler”的项目。它有一个名为“家”的应用程序。
我有以下网址映射:
./debtSettler/debtSettler/urls.py:
urlpatterns = [
path('home/', include('home.urls')),
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='home/')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
./debtSettler/home/urls.py:
app_name = 'home'
urlpatterns = [
path('', views.index, name='index'),
path('clubs/', views.ClubListView.as_view(), name='clubs'),
]
和意见:
./debtSettler/home/views.py:
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
num_clubs = Club.objects.all().count()
# The 'all()' is implied by default.
num_members = Member.objects.count()
context = {
'num_clubs': num_clubs,
'num_members': num_members,
}
# Render the HTML template index.html with the data in the context variable
return render(request, 'index.html', context=context)
class ClubListView(generic.ListView):
model = Club
def get_context_data(self, **kwargs):
# Call the base implementation first to get the context
context = super(ClubListView, self).get_context_data(**kwargs)
# Create any data and add it to the context
context['some_data'] = 'This is just some data'
return context
在模板中,我有两个给出错误的网址:
<a href=" {% url 'index' %} ">Home</a>
<a href=" {% url 'clubs' %} ">All clubs</a>
找不到“索引”的反向。 “索引”不是有效的视图函数或模式名称。
如果我添加 my_app:my_view,它会按预期工作:
家 所有俱乐部
但我计划在应用程序中进一步做更多的 url 映射,所以我想了解我在 url 上做错了什么。 在我看来,我正在做的事情与教程非常相似。
【问题讨论】: