Django Url direct error - django

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

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

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

Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name

I want to use the default django.contrib.auth.views for resetting passwords with email confirmation. All this code is on urls.py :
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
app_name = 'houses'
urlpatterns = [
# Root and details page
url(r'^$', views.index, name='index'),
url(r'^(?P<house_id>[0-9]+)/$', views.view_house, name='index'),
# Register / login / logout
url(r'^register/$', views.UserFormView.as_view(), name='register'),
url(r'^login/$', views.login_user, name='login_user'),
url(r'^logout/$', views.logout_user, name='logout_user'),
# User profiles and edit profiles
url(r'^profile/$', views.view_profile, name='view_profile'),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
# ---- ERRORS ARE HERE ---- change / reset passwords
url(r'^change_password/$', views.change_password, name='change_password'),
url(r'^password_reset/$', auth_views.password_reset,
{'post_reset_redirect': 'houses:password_reset_done',}, name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),
url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm, name='password_reset_confirm'),
]
No matter what I try i keep getting:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Is it because my app name is houses? I've been trying things for hours with no luck.
This error is because Django expects to find the url password_reset_complete in the project urls and not in the app urls, so for you to keep that url in the app you need to rewrite the template password_template_email.html and passes it in the url password_reset pass the param email_template_name:
path('reset_password/',
auth_views.PasswordResetView.as_view(
template_name="users/registration/password_reset.html",
email_template_name = 'users/registration/password_reset_email.html',
success_url=reverse_lazy('users:password_reset_done')),
name="reset_password"),
and in the template password_reset_email, pass
{% autoescape off %}
To initiate the password reset process for your Account {{ user.email }},
click the link below:
{{ protocol }}://{{ domain }}{% url 'youapp:password_reset_confirm' uidb64=uid token=token %}
If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.
{% endautoescape %}
I hope I helped!
If you are using reverse url in template
{% url 'houses:password_reset_confirm' uidb64=<uidb64> token=<token> %}
and if you using reverse url in python code then use
reverse('houses:password_reset_confirm', args=(<uidb64>,<token>,))
Here, <uidb64> means uidb64 value
<token> means token value
I had the same error before, to fix this I just wrote the URLs of the reset password option in the main URLs file (project_name/urls.py) and I've add in the templates folder a folder named "registration" that has inside a file named password_reset_email.html ( it contains the email that will be sent to the user, something like:
{% autoescape off %}
To initiate the password reset process for your Account {{ user.email }},
click the link below:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.
{% endautoescape %}
). you can check my code on github if you want to see it clearly https://github.com/Ninou01/customer-relationship-management-system/blob/master/crm1/urls.py

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

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.