I don't know what I'm doing wrong, but when I try to change the password it doesn't change it and it doesn't give any errors.
urls.py
from django.contrib.auth.views import logout,password_change,password_change_done
...
url(r'^change_password/?$',password_change, name='password_change'),
url(r'^password_changed/?$',password_change_done, name='password_change_done'),
url(r'^logout/?$',logout, name='logout'),
password_change_form.html
<form action="{% url 'password_change_done' %}" method="post">
{% csrf_token %}
{% bootstrap_form form layout="inline" form_group_class="form-group col-md-6" %}
<div class="clearfix"></div>
{% buttons %}
<button type="submit" name="save" class="btn btn-primary">{% bootstrap_icon "plus" %} {% trans 'save' %}</button>
{% endbuttons %}
</form>
When I click on save, it shows the template "password_change_done.html" but the password hasn't been changed. However, there are no errors in the console and I don't know what fails.
Thank you all
The form action should be password_change not password_change_done:
<form action="{% url 'password_change' %}" method="post">
Django will automatically do the redirect to password_change_done for you once the password change is successful.
Related
I am trying to take help from this tutorial and my own thing. It's supposed to be a marketplace.
I have an app called Creator (vendors)
I am trying to let the users add new products by themselves using
views.add_product, add_product.html, and forms.py
when I try to submit the add_product form, it gives this error
Error
Page not found (404) Request Method: POST Request URL: http://127.0.0.1:8000/creator/
(I am not trying to redirect it to this page but rather 'url 'creator_overview''
Project's urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls')),
path('creator/', include('creator.urls')),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
creator/urls.py
urlpatterns = [
path('sell_on_cosloo/', sell_great_things, name='creator_signup'),
path('creator_overview/',creator_overview , name='creator_overview'),
path('add_product/',add_product , name='add_product'),
...
]
creator/views.py
#login_required
def add_product(request):
if request.method == 'POST':
form = ProductForm(request.POST, request.FILES)
if form.is_valid():
product = form.save(commit=False)
product.creator = request.user.creator
product.slug = slugify(product.title)
product.save()
return redirect('creator_overview')
else:
form = ProductForm()
return render(request, 'creator/add_product.html', {'form': form})
creator/forms.py
<form method="post" action="." enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<div class="field">
<div class="control">
<button class="button is-dark is-uppercase">Submit</button>
</div>
</div>
</form>
add_product.html
{% extends 'core/base.html' %} {% block title %}Add product | {% endblock %} {%
block content %}
<h1 class="title">Add product</h1>
<form method="post" action="." enctype="multipart/form-data">
{% csrf_token %} {{ form.as_p }}
<div class="field">
<div class="control">
<button class="button is-dark is-uppercase">Submit</button>
</div>
</div>
</form>
{% endblock %}
Peace!! :))
Edit : Okay quick update: I think its something related to the images, I removed the image field from the form and now it seems to be working as its supposed to
The problem is the action attribute in the form.
Instead of :
<form method="post" action="." enctype="multipart/form-data">
Here the form will try to be submitted on http://127.0.0.1:8000/creator/ url that is not correct
Try this :
<form method="post" action="{% url 'add_product' %}" enctype="multipart/form-data">
The correct url is http://127.0.0.1:8000/creator/add_product/ as defined in the creator/urls.py file
I saw a tutorial and I implemented a password resetter through email. The email sending part works fine and I get the email after clicking it I get redirected to reset password page. But after I give the new password and click Submit it gets redirected to the login page but the password is not getting reset.
urls.py
path('password-reset/',auth_views.PasswordResetView.as_view(template_name='password_reset.html'),name='password_reset'),
path('password-reset/done/',auth_views.PasswordResetDoneView.as_view(template_name='password_reset_done.html'),name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html'),name='password_reset_confirm'),
path('password-reset-complete/',auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html'),name='password_reset_complete'),
password_reset_confirm.py
{% extends "base.html" %}
{% load static %}
{% block head_block %}
<link rel="stylesheet" type="text/css" href="{% static 'index.css' %}">
{% endblock head_block %}
{% block body_block %}
{% csrf_token %}
<form method="post" action="{% url 'reg_sign_in_out:user_login' %}">
{% csrf_token %}
<input type="password" name="" id="">
<input type="submit" value="Reset">
</form>
{% endblock %}
Any idea where the problem is?
I think that you have to put in your form the action of your post, for example if you have to go to the password-reset/done/ URL, you have to add
<form method="post" action="password-reset/done/" >
{% csrf_token %}
<input type="password" name="" id="">
<input type="submit" value="Reset">
</form>
Because if you don't put the action, it will redirect to the same page
The problem was a redirecting issue. Its fixed.
I'm trying to get into Django and got stuck with DeatailView and RedirectView
I made the following template for rendering posts with enabling post detailed view and like button. When I click on either Like or Detail button none of the requests is sent. I tried changing different methods, but that didn't help either. I trying to get whether I was mistaken in the template or in view logic.
{% extends 'base.html' %}
{% block content %}
{% for post in object_list %}
<div class="card" style="width: 40rem;">
<img src = "{{ post.image.url }}" style="height: 40rem;">
<div class="card-body">
<h5 class="card-tittle">{{post.author.username}}</h5>
<p class="card-text">{{ post.description }}</p>
<p class="card-text">Likes : {{ post.likes.count }}</p>
<p class="id">Post_id: {{ post.id }}</p>
<div class="btn-group-vertical">
{% if user.is_authenticated %}
<form action="{% url 'like_post' %}" method="POST">
{% csrf_token %}
<button type="button" class="btn btn-secondary">Like</button>
</form>
{% endif %}
<form action="{% url 'detail' pk=post.id %}" method="GET">
<button type="button" class="btn btn-secondary">Detail</button>
</form>
<!-- <button type="button" class="btn btn-secondary">down</button> -->
</div>
</div>
</div>
{% endfor %}
{% endblock %}
Posts views.py file
class PostDetail(DetailView):
model = Post
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
print(context.items())
return context
class PostLikeUpvote(RedirectView):
def get_redirect_url(self, *args, **kwargs):
post_id = kwargs.get('id')
post = get_object_or_404(Post, id=post_id)
user = self.request.user
if user.is_authenticated():
post.likes.add(user)
url_redirect = post.get_redirect_url()
return url_redirect
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('new/', PostCreate.as_view(), name='new'),
path('', PostList.as_view(), name='list'),
path('posts/', PostList.as_view(), name='list'),
path('like_post/', PostLikeUpvote.as_view(), name='like_post'),
path('posts/<pk>/', PostDetail.as_view(), name='detail'),
path('bot/webhook', csrf_exempt(BotView.as_view())),
]
Any comments are appreciated.
In order to submit the form, the type of the button should be submit, not button:
{% if user.is_authenticated %}
<form action="{% url 'like_post' %}" method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-secondary">Like</button>
</form>
{% endif %}
otherwise it is a simple button that can for example run a JavaScript function, but it will not (automatically) submit the form. Of course you can write a JavaScript function to submit the form, but unless you have a specific reason for that, it is better to let the browser do the work for you.
I am deleting a post using generic DeleteView. On clicking the link for delete it returns
django.urls.exceptions.NoReverseMatch: Reverse for 'postdelete' with
no arguments not found. 1 pattern(s) tried:
['posts/(?P<id>[0-9]+)/delete/$']
I tried placing the before and after /delete/
#urls.py
path('posts/<int:id>/delete/',blogpostDelete.as_view(),name='postdelete'),
#DeleteView
class blogpostDelete(DeleteView):
success_url='/posts/'
template_name="blog/delete.html"
def get(self,request,id) :
Blogpost = get_object_or_404(blogpost,id=id)
return self.render_to_response({'id':id})
#link in template
<a href={% url "postdelete" id=id %}>Delete</a>
#delete.html
{% block content %}
<form action={% url "postdelete" %} method="post">
{% csrf_token %}
<p>Are you sure you want to delete "{{ id }}"?</p>
<input type="submit" value="Confirm">
</form>
{% endblock %}
You're not passing id to the url.
Try something like this
<form action={% url "postdelete" id="{{id}}" %} method="post">
In Delete View No need to pass id in html form action
Doing Like this:
<form method="post">
{% csrf_token %}
<p>Are you sure you want to delete "{{ id }}"?</p>
<input type="submit" value="Confirm">
</form>
I am making an app in django in which i have used the django built in authentication system. Both login and logout links are present in the navbar. I want to make the logout link appear only when the user has logged in and not at all times. How do i do that?
code snippet of project/urls.py:
urlpatterns = [
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name='login'),
url(r'^logout/$', views.logout, {'next_page': '/home'}), ]
code snippet of login.html;
<div class="container">
<section id="content">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form action="{% url 'login' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h1>Login Form</h1>
<div class="imgcontainer">
<img src="{% static 'student/patient.jpg' %}" alt="Avatar" class="avatar">
</div>
<div class="username">
{{ form.username.label_tag }}
{{ form.username }}
</div>
<div class="password">
{{ form.password.label_tag }}
{{ form.password }}
</div>
<div class="submitb">
<input type="submit" value="Log In" name="mybtn">
</div>
<div class="resetb">
<input type="submit" value="Reset">
Forgot password?
</div>
<input type="hidden" name="next" value="{{ next }}" />
</form>
code snippet of base.html(only the navbar is shown):
<ul>
<li><a class="active" href="/home">Home</a></li>
<li>About </li>
<li>Sign up</li>
<li>Doctor's login</li>
<li>Patient's login</li>
<li>FAQs</li>
<li>Contact us</li>
<li>Reviews</li>
<li>Logout</li>
</ul>
Thanks for your help in advance.
Put the logout list item in an if block that checks that the user is authenticated like so:
<ul>
...
{% if request.user.is_authenticated %}
<li>Logout</li>
{% endif %}
</ul>
Although using the variable request.user.is_authenticated is one way. To make it simpler create HTML pages such that login and logout button are disintegrated.
Pages which appear after login should only contain the logout option/button. This will smoothen your development process.