我在做一个 udemy 课程,但它是在 django 1.11 上,我试图用 2.0 来做。练习是使用组,用户和帖子进行社交网络克隆,该练习具有3个主要应用程序,帐户,组和帖子。这是文件路径:
- Django exercize
- Django exercize
- groups app
- template
- groups app
- html files
- urls.py
- views.py
- posts app
- template
- posts app
- html files
- urls.py
- views.py
- accounts app
- template
- accounts app
- html files
- urls.py
- views.py
在我尝试加载"组列表"或"创建新组"的某个时候,服务器会显示此错误:
NoReverseMatch at /groups/
'post' is not a registered namespace
这是在组文件夹上创建和列表 views.py
class CreateGroup(LoginRequiredMixin,generic.CreateView):
fields = ('name','description')
model = Group
template_name = 'groups/groups_form.html'
class ListGroups(generic.ListView):
model = Group
template_name = 'groups/groups_list.html'
这是组文件夹 urls.py
from django.urls import path
from . import views
app_name = 'groups'
urlpatterns = [
path('',views.ListGroups.as_view(),name='all'),
path('new/',views.CreateGroup.as_view(),name='create'),
path('post/in/<slug:slug>',views.SingleGroup.as_view(),name='single'),
path('join/<str:slug>/',views.JoinGroup.as_view(),name='join'),
path('leave/<str:slug>/',views.LeaveGroup.as_view(),name='leave'),
]
这是 django 主文件夹上的 urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.HomePage.as_view(), name='home'),
path('accounts/', include('accounts.urls',namespace='accounts')),
path('accounts/',include('django.contrib.auth.urls')),
path('test/',views.TestPage.as_view(),name='test'),
path('thanks/',views.ThanksPage.as_view(),name='thanks'),
path('posts/',include('posts.urls', namespace='posts')),
path('groups/',include('groups.urls',namespace='groups')),
]
这是组应用程序文件夹上的 html tamplate
{% extends "groups/groups_base.html" %}
{% block pregroup %}
<div class="col-md-4">
<div class="content">
{% if user.is_authenticated %}
<h2>Bienvenido!! <a href="{% url 'post:for_user' username=user.username %}"> @{{ user.username }}</a></h2>
{% endif %}
<h2>Grupos</h2>
<p>Bienvenido a la pagina de grupos!!</p>
</div>
{% if user.is_authenticated %}
<a href="{% url 'group:create' %}" class="btn btn-md btn-fill btn-warning">Crear Grupo!!</a>
{% else %}
{% endif %}
</div>
{% endblock pregroup %}
{% block group_content %}
<div class="col-md-8">
<div class="list-group">
{% for group in object_list %}
<a class="list-group-item" href="{% url 'group:single' slug=group.slug %">
<h3 class="title list-group-item-heading">{{ group.name }}</h3>
<div class="list-group-item-text container-fluid">
{{ group.description_html|safe }}
<dive class="row">
<div class="col-md-4">
<span class="badge">{{ group.members.count }}</span>
member{{ group.members.count|pluralize }}
</div>
<div class="col-md-4">
<span class="barde">{{ group.post.count }}</span>
post {{ group.posts.count|pluralize }}
</div>
</dive>
</div>
</a>
{% endfor %}
</div>
</div>
{% endblock group_content %}
在 url 中,你有命名空间posts
:path('posts/',include('posts.urls', namespace='posts')),
。因此,您应该在模板中使用posts
而不是post
: <a href="{% url 'posts:for_user' username=user.username %}">
.