NoReverseMatch at /; error at line 0 - django

I'm getting the below error and I have no idea why... I swear everything was working fine yesterday... I'm using python 3.5 and django 1.9. I went through most of the NOReverseMatch posts, but I couldn't find anything... please help
"Reverse for 'xxx' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
and
In template F:\Django-tutorial\src\landing_pages\templates\landing_pages\home.html, error at line 0
home.html should be displayed as a main page
My projects url.py:
urlpatterns = [
url(r'^', include('landing_pages.urls')),
url(r'^admin/', admin.site.urls),
url(r'^in/', include('web_monitor.urls')),
]
landing_pages.urls:
from django.conf.urls import url
from landing_pages.views import home, contact, about, cost
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^contact/$', contact, name='contact'),
url(r'^about/$', about, name='about'),
url(r'^cost/$', cost, name='cost'),
]
Home.html:
{% extends 'landing_pages/base.html' %}
{% load crispy_forms_tags %}
{% block jumbotron %}
<!-- Main component for a primary marketing message or call to action -->
<div class="jumbotron">
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-3">
<form method="POST" action="">
{% csrf_token %}
{{ form|crispy }}
<input class="btn btn-primary" type="submit" value="Submit">
</form>
</div>
</div>
{% endblock %}
base.html:
{% load staticfiles %}
<html lang="en">
<head>
{% include 'landing_pages/head_css.html' %}
</head>
<body>
{% include 'landing_pages/nav_bar.html' %}
<div class="container">
{% block jumbotron %}
{% endblock %}
{% block content %}
{% endblock %}
</div> <!-- /container -->
</body>
</html>
nav_bar.html is just a nav bar, where i reference links like this:
<li>Cost</li>

Related

NoReverseMatch at /account/login/ (Trying to use Class based authentication views)

I am trying to use Django's class based authentication views and am getting the following error when attempting to access the login view:
NoReverseMatch at /account/login/
Reverse for 'register' not found. 'register' is not a valid view function or pattern name.
Error during template rendering
In template /Users/justin/Desktop/Programming/Python/django_book/social/website/account/templates/base.html, error at line 0
All authentication templates are stored at account/templates/registration/ and dashboard.html is stored at account/templates/account/, here is the code:
account/urls.py:
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', views.dashboard, name = 'dashboard'),
path('login/', auth_views.LoginView.as_view(), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(), name = 'logout'),
]
login.html:
{% extends "base.html" %}
{% block title %}Log-in{% endblock %}
{% block content %}
<h1>Log-in</h1>
{% if form.errors %}
<p>
Your username and password didn't match.
Please try again.
</p>
{% else %}
<p>Please, use the following form to log-in. If you don't have an account register here</p>
{% endif %}
<div class="login-form">
<form action="{% url 'login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Log-in"></p>
</form>
</div>
{% endblock %}
base.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title%}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<div id="header">
<span class = 'logo'>Bookmarks</span>
{% if request.user.is_authenticated %}
<ul class = 'menu'>
<li {% if section == "dashboard" %}class="selected"{% endif %}>
My dashboard
</li>
<li {% if section == "images" %}class="selected"{% endif %}>
Images
</li>
<li {% if section == "people" %}class="selected"{% endif %}>
People
</li>
</ul>
{% endif %}
<span class = 'user'>
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name }},
Logout
{% else %}
Login
{% endif %}
</span>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
Note sure if this is needed but the account/views.py with the dashboard view:
from django.contrib.auth.decorators import login_required
#login_required
def dashboard(request):
return render(request,
'account/dashboard.html',
{'section': 'dashboard'})
and dashboard.html:
{% extends "base.html" %}
{% block title %}Dashboard{% endblock %}
{% block content %}
<h1>Dashboard</h1>
<p>Welcome to your dashboard.</p>
{% endblock %}
I am following the book 'Django 2 By Example' and at this point I believe I have directly copy and pasted the code from here (https://github.com/PacktPublishing/Django-2-by-Example/tree/master/Chapter04) to try and fix this error, but I am still getting. I should note that I am using Django 3.2.6 and am not sure if this is causing it. Thanks for any help with this.

why my search bar is not working in Django?

created a simple search in the Django blog but it's not working. why search bar is not working in a blog web app ? its nothing to search in the search bar when searching something in the search box.
- urls.py
urlpatterns = [
path('', views.home, name='home'),
path('about', views.about, name='about'),
path('contact', views.contact, name='contact'),
path('search', views.search, name='search'),
]
- views.py
def search(request):
query = request.GET['query']
allPosts = Post.objects.filter(title__icontains=query)
params = {'allPosts': allPosts}
return render(request,'home/search.html', params)
- search.html
{% extends 'base.html' %}
{% block title %} Search Results {% endblock title %}
{% block blogactive %}active{% endblock blogactive %}
{% block body %}
<div class="container my-3">
<h2>Search Results</h2>
{% for post in allposts %}
<div class="row no-gutters border rounded overflow-hidden flex-md-row my-4 shadow-sm h-md-250 position-relative">
<div class="col p-4 d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-primary">Article by {{post.author}}</strong>
<h3 class="mb-0">{{post.title}}</h3>
<div class="mb-1 text-muted">{{post.datetime}}</div>
<p class="card-text mb-auto">{{post.content | truncatechars:500}}</p>
<div class='my-2'>
Continue reading
</div>
<div class="col-auto d-none d-lg-block">
</div>
</div>
</div>
{% endfor %}
{% endblock body %}
There is a typo in your code. In template you are using allposts(all lower case) where you are passing allPosts from context, ie: params = {'allPosts': allPosts}. So you need to change either one of them, like change in context:
params = {'allposts': allPosts}
And one improvement suggestion, replace href="/blog/{{post.slug}}" in the template with href="{% url 'url_name' %}". More information can be found in url tag documentation.

why I can't get the PasswordResetDoneView page?

when I submit my Email form from PasswordResetView page the email appears via URL as following: http://127.0.0.1:8000/account/password-change/?email=medoabdin%40gmail.com and not directing me to PasswordResetDoneView page and I get no error.
how can I show PasswordResetDoneView message page
urls.py
from django.urls import path
from . import views
from django.contrib.auth.views import (
LoginView,
LogoutView,
PasswordResetView,
PasswordResetDoneView)
from django.urls import reverse_lazy
app_name = 'account'
urlpatterns = [
# /account/login/
path('login/', LoginView.as_view(template_name='account/login.html'), name='login'),
# /account/logout/
path('logout/', LogoutView.as_view(template_name='account/logout.html'), name='logout'),
# /account/register/
path('register/', views.register, name='register'),
# /account/view-profile/
path('view-profile/', views.view_profile, name='view_profile'),
# /account/password-change/
path('password-change/', PasswordResetView.as_view(template_name='account/password_change_view.html', success_url=reverse_lazy('account:password_change_done'), email_template_name='account/reset_password_email.html'), name='password_change'),
# /account/password-change/done/
path('password-chane/done/', PasswordResetDoneView.as_view(template_name='account/password_change_done.html'), name='password_change_done'),
]
password_change_done.html
{% extends 'base.html' %}
{% block title %} Success Message {% endblock %}
{% block body %}
{% if not user.is_authenticated %}
<div class="password-change-done">
<div class="container">
<h1 class="text-primary"> The Password Has Been Sent </h1>
<p class="lead text-success"> Check your email and following the rest of record until you can go back your own email. </p>
<p class="lead text-success"> You need to follow the instructions to get what you want. </p>
</div>
</div>
{% endif %}
{% endblock %}
password_change_view.html
{% extends 'base.html' %}
{% block title %} Chnage your Passowrd {% endblock %}
{% block body %}
{% if not user.is_authenticated %}
<div class="password-change">
<div class="container">
<div class="my-form">
<form class="post">
{{ form.as_p }}
<button type="submit" class="btn btn-success">Go</button>
</form>
</div>
</div>
</div>
{% endif %}
{% endblock %}
settings.py
urlpatterns = [
path('', include('index.urls')),
path('account/', include('account.urls')),
path('admin/', admin.site.urls),
path('', include('django.contrib.auth.urls')),
]
Put method="post" in form tag and also {% csrf_token %}, probably because of GET method same view is rendering and form_valid is not invoking.

NoReverseMatch at /login/ [duplicate]

This question already has answers here:
Error "Reverse for 'django.contrib.auth.views.login' not found" [duplicate]
(2 answers)
Closed 5 years ago.
NoReverseMatch at /login/ is the error I'm getting from the console, I have a feeling this is something to do with the linking that is going on between urls.py but I can't figure out which part of it is wrong. Any help would be greatly appreciated on this.
The full error from the console is beneath
Reverse for 'django.contrib.auth.views.login' not found.
'django.contrib.auth.views.login' is not a valid view function or pattern name.
urls.py
from django.contrib import admin
from polls import views as polls_views
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^calendar/', include('calendarium.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^$', polls_views.home, name='home'),
url(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': 'login'}, name='logout'),
url(r'^signup/$', polls_views.signup, name='signup'),
]
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}OnNote{% endblock %}</title>
</head>
<body>
<header>
<h1>My Site</h1>
{% if user.is_authenticated %}
logout
{% else %}
login / signup
{% endif %}
<hr>
</header>
<main>
{% block content %}
{% endblock %}
</main>
</body>
</html>
login.html
{% extends 'base.html' %}
{% block title %}Login{% endblock %}
{% block content %}
<div id="content-container" class="container p-none">
<div class="lgn-container col-lg-8">
<form id="login-form" method="post"
action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}
<table class="table">
<tr>
<td><label for="id_username">Username</label></td>
<td><input id="id_username" name="username"
type="text" class="form-control"></td>
</tr>
<tr>
<td><label for="id_password">Password</label></td>
<td><input id="id_password" name="password"
type="password" class="form-control"></td>
</tr>
</table>
{% if form.errors %}
<p class=" label label-danger">
Your username and password didn't match.
Please try again.
</p>
{% endif %}
<input type="submit" value="Login"
class="btn btn-primary pull-right" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
</div>
{% endblock content %}
Can anyone point out what I'm doing wrong here? I think it's something to do with my urls.py but I can't figure out what, any help would be greatly appreciated
<form id="login-form" method="post" action="">
in login.html
you dont need the action, simply keeping it to post to itself will work
You can either use form with no action or change {% url 'django.contrib.auth.views.login' %} to {% url 'login' %}
By the way, standard login view is deprecated and you will get a warning.
It's better to use LoginView.as_view() or subclass it if you want to change the default template_name (which is 'registration/login.html').

Django, trying to change the class attribute of an extended nav bar

I created a "base.html" template to hold my nav bar and other elements that are recurrent on my website.
When I extend this template and put values into my blocks one of them doesn't want to work properly.
In my code I try to set the class value of my nav element to "active" depending on which page I'm in (it might not be the best solution). This block never works.
Thank you for your help :)
urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^addProduct/$', views.addProduct, name='addProduct'),
]
views.py
def index(request):
latest_product_list = Product.objects.all().order_by('-id')[:5]
return render(request, 'main/index.html', {'latest_product_list': latest_product_list})
def addProduct(request):
# Do things
return render(request, 'main/add_product.html', {'form':form})
base.html
<!DOCTYPE html>
<html lang="fr">
<head>
#header informations
</head>
<body>
<div class="container-fluid">
<div class="row">
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
</div>
<ul class="nav nav-pills" role="tablist">
<li role="presentation" class="{% block navbar_class-index %}{% endblock %}">Index</span></li>
<li role="presentation" class="{% block navbar_class-addProduct %}{% endblock %}">Ajouter un produit</li>
</ul>
<div class="row" id="content">
{% block content %}{% endblock content %}
</div>
</div>
</body>
index.html
{% extends "base.html" %}
{% block navbar_class-index %}active{% endblock %}
{% block content %}
<div class="col-md-12">
## Here is my content
</div>
{% endblock content %}
add_product.html
{% extends "base.html" %}
{% block navbar_class-addProduct %}active{% endblock %}
{% block content %}
<div class="col-md-12">
<form action="{% url 'main:addProduct' %}" method="post">
{% csrf_token %}
<label> Entrez votre produit et cliquez sur Add Product.</label><br/><br/>
<label> Produit : </label>{{form.form_product_url}}
<input type="submit" value="Add Product" />
</form>
</div>
{% endblock content %}
I guess your base.html file is not in the correct folder !