django url resolvers reverse lookup - django

NoReverseMatch
I'm trying to make a basic website with the following urls
/congregation/ - home page
/congregation/archives - archives for that page
There are multiple 'congregations', each with an archive page.
I have the following url patterns:
church/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls, name='admin'),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
url(r'^', include('congregation.urls', namespace='congregation')),
]
church/congregation/urls.py
page_patterns = [
url(r'^$', CongregationView.as_view(), name='home'),
url(r'^archive/', SermonList.as_view(), name='archive')
]
urlpatterns = [
url(r'^(?P<congregation>\w{3,20})/', include(page_patterns, namespace='page'))
]
The pages display as I expect they should, but if I use links in header nav-bar, reverse lookup fails with this message
NoReverseMatch at /spokane/archive/
Reverse for 'archive' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P<congregation>\\w{3,20})/archive/']
The error is caused by an invalid reverse match in the template
<li class="{% if request.resolver_match.url_name == 'archives' %}active{% endif %}">
Archives</li>
I'm sure it's simple, but I'm having difficulty finding the solution.
Thank you!

Your urlpattern url(r'^(?P<congregation>\w{3,20})/', include(page_patterns, namespace='page')) requires a congregation keyword. When you call your url resolver with {% url 'congregation:page:archive' %}, you are not providing a congregation keyword.
Ty{% url 'congregation:page:archive' congregation=whatever %}

Related

Why NoReverseMatch at occurred on django 2.0

Error says :
Reverse for 'list' not found. 'list' is not a valid view function or pattern name.
My code are on below.
html template where error is in:
{% block more_posts %}<button type="button" href="{% url 'website:list' %}">more posts</button>{% endblock %}
my_project/urls.py:
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('website.urls')),
]
website/urls.py:
app_name = 'website'
urlpatterns = [
re_path(r'^about/$', TemplateView.as_view(template_name='website/about.html'), name='list'),
re_path(r'^$', views.main, name='main'),
]
Is there are something wrong with my url namespace settings?
add namespace
re_path(r'^', include('website.urls',namespcae="website")),

Django URL in template NoReverseMatch

I keep getting a NoReverseMatch in my base template, though I've specified a namespace and have put the proper name.
Error:
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['forum/|^forums/$']
Main urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from forum.views import main_home
urlpatterns = [
url(r'^$', main_home, name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/|^account/', include('accounts.urls',
namespace='accounts')),
url(r'^forum/|^forums/', include('forum.urls', namespace='forum')),
]
Forum urls.py:
from django.conf.urls import url
from forum import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
From my template:
<a class="nav-link" href="{% url 'forum:home' %}">Forum</a>
The docs for reverse() say You cannot reverse url patterns that contain alternative choices using |.
In this case, you could change the URL to:
url(r'^forums?/', include('forum.urls', namespace='forum')),
However, it might be better to choice a single URL /forums/ or /forum/. Having a single URL can be better for SEO.

Django Url direct error

My Django urlpatterns:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login', views.login_page, name='login'),
url(r'^logout', views.logout_page, name='logout'),
url(r'^register', views.register_page, name='register'),
url(r'^create_book', views.create_book, name='create_book'),
url(r'^^(?P<book_id>[0-9]+)/$', views.book_details, name='book_details'),
url(r'^^(?P<book_id>[0-9]+)/create_entry$', views.create_entry, name='create_entry'), ]
I am trying to do this:
{% url 'book:book_details' book.pk %}
It gives this error:
NoReverseMatch at /
Reverse for 'book_details' with keyword arguments '{'book_id': 1}' not found. 1 pattern(s) tried: ['^(?P<book_id>[0-9]+)/$']
But going to localhost:8000/ <some id> directly in the browser works
How do I write the {% url %} part so that it goes to /(some id)
change this line in template
{% url 'book:book_details' book.pk %}
to
{% url 'book_details' book.pk %}
and remove the extra caret sign ^ from the url
url(r'^(?P<book_id>[0-9]+)/$', views.book_details, name='book_details'),
url(r'^(?P<book_id>[0-9]+)/create_entry$', views.create_entry, name='create_entry'),

NoReverseMatch at /index

I am trying to understand why I am seeing a NoReverseMatch error when trying to use Django Contact Form.
The error occurs when I add a link using the following syntax to index.html:
<h3>Contact</h3>
If I use the following hardcoded syntax then no errors occur and the link to the contact form from index.html works as expected.
<h3>Contact</h3>
What I am trying to achieve is similar to what is shown the Django tutorial about removing hard coded urls.
The full error I see is:
NoReverseMatch at /index
Reverse for 'contact' with arguments '()' and keyword arguments '{}'
not found. 0 pattern(s) tried: []
In case of need, my abbreviated urls.py is:
urlpatterns = patterns('',
...
url(r'index$', views.index, name='index'),
...
url(r'^contact/', include('contact_form.urls')'),
)
I know I am missing something obvious!
It's because there is no url with name contact.
url(r'^contact/', include('contact_form.urls')'),
is url that will map all urls starting with contact to the contact_form.urls. Official docs don't say how to access contact view, but with basic understanding of django we can do something like this:
urlpatterns = patterns('',
...
url(r'index$', views.index, name='index'),
...
url(r'^contact/', include('contact_form.urls', namespace='contacts')),
)
and the in the template:
<h3>Contact</h3>
contact_form url name is found in the source code of the module.
when you use {% url 'contact' %} in template, the 'contact' is actually the name of route. In your url patterns there is no route with this name. You should include something like this into your contact_forms.urls.py:
url(r'$', views.index, name='contact_index')
Also, you should change "contact/" pattern to:
url(r'^contact/', include('contact_form.urls', namespace='contacts'))
and then use this when creating link in template:
{% url 'contacts:contact_index' %}

How can I get {% url logout %} to work in my Django template?

My Django app's site-wide urls.py file looks like this:
urlpatterns = patterns('',
url(r'^$', include('myApp.urls')),
url(r'^admin/', include(admin.site.urls)),
url (
r'^accounts/register/$',
RegistrationView.as_view(form_class=extendedRegistrationForm),
),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^', include('myApp.urls')),
)
I also have a urls.py specific to myApp but I have not shown that here because I don't think it's relevant.
In my template file, I have this:
{% url logout %}
It gives me this error:
'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
When I change the template to:
{% url 'logout' %}
It gives me the following error:
Reverse for 'logout' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How do I put a link to logout in my view?
Add a logout url like this:
url(r"^logout/$", "django.contrib.auth.views.logout_then_login",
name="logout"),
The correct template syntax is:
{% url 'logout' %}
You didn't include django.contrib.auth's URLs in your urls.py, so there is no logout URL.
Try adding something like
url(r'^auth/', include('django.contrib.auth.urls')),
In django 3.0 {% url 'admin:logout' %} works perfect.