Why NoReverseMatch at occurred on django 2.0 - django

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

Related

Modified urlpatterns in django

I modified url in django, i can use tag {% url 'student-register' %} in template but when i call {% url 'student-reg' %} it return error reverse-not found, anyone can describe what happened ? this is my code :
from django.urls import path, include
from .views.home import *
from .views.teacher import *
urlpatterns = [
path('', ViewHomePage.as_view(), name='home'),
path('logout', LogoutAccount.as_view(), name='logout'),
path('teacher/', include(([
path('', ViewTeacherHome.as_view(), name='teacher'),
path('student-register', ViewStudentRegister.as_view(), name='student-register'),
], 'exam'), namespace='teacher')),
path('student/', include(([
path('', ViewTeacherHome.as_view(), name='student'),
path('student-reg', ViewStudentRegister.as_view(), name='student-reg'),
], 'exam'), namespace='student'))
]
Since you specify student and exam namespaces for nested urls list you should use:
{% url 'student:exam:student-reg' %}

A issue to use URL template tag in django

I am currently learning Django. When I apply a url template tag, I found that the output of the url tag is not what I expected. I have read the Django Documents, but it does not help.
{{ movie.title }}
from django.urls import path
from . import views
app_name = 'movies'
urlpatterns = [
path('', views.index, name='index'),
path('<int:movie_id>', views.detail, name='detail')
]
The output of the url tag is localhost/movies/%7B%%20url%20'movies:detail'%20movie.id%20%
which is not as I expected: localhost:8000/movies/1
Modify lines as follow:
{{ movie.title }}
...
path('movies/<int:movie_id>/', views.detail, name='detail')

%5E being inserted into reverse url

I'm having a problem with a url resolving as it should. It's resolving as
http://localhost:8000/%5Ewebsites/?value=1&id=1 when it should be resolving as
http://localhost:8000/websites/?value=1&id=1
I've got the following urls.py inside of an app.
app_name = 'websites'
urlpatterns = [
url(r'^$', website_views.homepage, name="homepage"),
url(r'^blog/$', website_views.blog, name="blog"),
url(r'^blog/(?P<id>\d+)/$', website_views.blogpost, name="blogpost"),
]
I've got this inside of my project urls.py file:
urlpatterns = [
url(r'^websites/', include('websites.customerurls')),
The link that is resolving incorrectly is this:
<a href="{% url 'websites:homepage' %}?value=1&id=1" target="_blank">
Any help would be appreciated! Thanks!
I have to delete the ^ in urlpatterns = [ url(r'^websites/', include('websites.customerurls')),. It now resolves without the %5E and the page loads correctly.

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

django url resolvers reverse lookup

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