2

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'),
)

2 Answers 2

5

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'),
)
Sign up to request clarification or add additional context in comments.

3 Comments

@Jinx Glad I could help. You should upvote and accept the answer then. :)
Its something which many new contributors are unaware of what to do when they receive a correct answer. Hence during initial days most of the new contributors are made aware about what to do and how to do.
haha it's ok. I used to try to upvote when I was under 15 reputation, but I wasn't able to do that.
0

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

2 Comments

I tried, it won't work that way. That's why I added namespace='account'.
look below is my own configuration and it works : urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^api/v1.0/', include('api.urls')), ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.