SpellTheif
SpellTheif

Reputation: 715

Django - URL and namespace

I'm building a Django template in the Book app and using URL tag to redirect to the Account app's URLs. But it says account' is not a registered namespace.

book.urls:

app_name = 'book'
urlpatterns = [
    path('', views.HomePageView.as_view(), name='home'),
    path('account/', include('account.urls', namespace='account'))
]

book.views:

class HomePageView(generic.TemplateView):
    template_name = 'book/home.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['all_books'] = Book.objects.all()
        return context

templates/book/home.html:

<div id="register">
    <p>
        <a href="{% url 'account:register' %}"> Sign Up </a>
    </p>
</div>

account/urls:

app_name='account'
urlpatterns=(
    path('register/', views.RegisterView.as_view(), name='register'),
    path('successful/', views.successful_created, name='successful'),
)

Upvotes: 2

Views: 1175

Answers (2)

Arghya Saha
Arghya Saha

Reputation: 5733

The problem which you are facing is mostly because you are trying to define account app from book app. What you need to do is

In the main project urls.py which is in the same directory as settings.py add both book and account app.

urlpatterns = [
    url(r'^book/', include('book.urls', namespace="book")),
    url(r'^account/', include('account.urls', namespace="account")),
]

And now your book.urls will look:

app_name = 'book'
urlpatterns = [
    path('', views.HomePageView.as_view(), name='home')
]

account/urls will look:

app_name='account'
urlpatterns=(
    path('register/', views.RegisterView.as_view(), name='register'),
    path('successful/', views.successful_created, name='successful'),
)

Upvotes: 5

Siyavash vaez afshar
Siyavash vaez afshar

Reputation: 1493

I believe if you remove namespace = account and just use path('account/', include('account.urls') it'll work fine.

Upvotes: 0

Related Questions