Django 2.0' post'不是已注册的命名空间

时间:2018-03-07 01:57:07

标签: django namespaces

我正在做一个udemy课程,但它是在django 1.11,我试图用2.0做。 练习是使用群组,用户和帖子进行社交网络克隆,exersize有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'

这是groups文件夹中的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')),
]

这是组app文件夹上的html模板

{% 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 %}

1 个答案:

答案 0 :(得分:0)

在网址中,您拥有名称空间postspath('posts/',include('posts.urls', namespace='posts')),。因此,您应该在模板中使用posts而不是post<a href="{% url 'posts:for_user' username=user.username %}">