Trying to change password using PasswordChangeView, but cannot get it working.
urls.py
from django.contrib.auth import views as auth_views
urlpatterns = [
path('profiles/settings/', update_profile, name='update_profile'),
path('profiles/settings/', auth_views.PasswordChangeView.as_view(template_name='accounts/settings.html'),
name='password_change'),
]
And i am trying to get the input fields correct in my html
<div class="tab-pane fade" role="tabpanel" id="password">
<form id="id_password_change_form" method="POST" class="form-signin">{% csrf_token %}
<div class="form-group row align-items-center">
<label class="col-3">Current Password</label>
<div class="col">
<input
type="password"
placeholder="Enter your current password"
name="old_password"
class="form-control"
id="id_old_password"
required="true" />
</div>
</div>
<div class="form-group row align-items-center">
<label class="col-3">New Password</label>
<div class="col">
<input
type="password"
placeholder="Enter a new password"
name="new_password1"
class="form-control"
id="id_new_password1"
required="true" />
<small>Password must be at least 8 characters long</small>
</div>
</div>
<div class="form-group row align-items-center">
<label class="col-3">Confirm Password</label>
<div class="col">
<input
type="password"
placeholder="Confirm your new password"
name="new_password2"
class="form-control"
id="id_new_password2"
required="true" />
</div>
</div>
{% for field in form %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-primary">Change Password</button>
</div>
</form>
There is no error, and it do not update the password as supposed to. according to PasswordChangeView, I should not need to alter anything.
I think you have a problem because of the wrong URLs definition, take a look at you urls.py:
from django.contrib.auth import views as auth_views
urlpatterns = [
path('profiles/settings/', update_profile, name='update_profile'),
path('profiles/settings/', auth_views.PasswordChangeView.as_view(template_name='accounts/settings.html'),
name='password_change'),
]
You always hit update_profile view instead of PasswordChangeView. I think this is typo.
Related
I'm running my code through validator and it's coming back with four errors which I don't know how to fix as the code has been imported from django forms.
Does anyone know how to fix these?
<div class="form-group">
<form method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="...">
<p><label for="id_username">Username:</label> <input type="text" name="username" maxlength="150" autofocus class="form-control" required id="id_username"> <span class="helptext">Required. 150 characters or
fewer. Letters, digits and #/./+/-/_ only.</span></p>
<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" class="form-control" maxlength="100" required id="id_first_name"></p>
<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" class="form-control" maxlength="100" required id="id_last_name"></p>
<p><label for="id_email">Email:</label> <input type="email" name="email" required id="id_email"></p>
<p><label for="id_password1">Password:</label> <input type="password" name="password1" autocomplete="new-password" class="form-control" required id="id_password1"> <span class="helptext">
<ul>
<li>Your password can’t be too similar to your other personal information.</li>
<li>Your password must contain at least 8 characters.</li>
<li>Your password can’t be a commonly used password.</li>
<li>Your password can’t be entirely numeric.</li>
</ul>
</span></p>
<p><label for="id_password2">Password confirmation:</label> <input type="password" name="password2" autocomplete="new-password" class="form-control" required id="id_password2"> <span class="helptext">Enter the same password as before, for verification.</span></p>
<div class="d-grid">
<button class="btn btn-dark ">Register</button>
</div>
</form>
</div>
I have used:
from django import forms
and in my views.py file I used:
from .forms import SignUpForm
my register.html page is as following:
{% extends 'base.html' %}
{% block title %}Register{% endblock %}
{% block content %}
<h1 class="my-4">Register</h1>
<div class="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<div class="d-grid">
<button class="btn btn-dark ">Register</button>
</div>
</form>
</div>
{% endblock %}
Using django for rendering user uploaded data from a "/blogs/createblog" form web page to "/blogs" webpage, but the image file uploaded by the user is not getting displayed in his/her blogpost ("/blogs") page. My settings.py, base level/urls.py, app/models.py, app/views.py and the html templates are something like this:
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
baseproject/urls.py
from django.contrib import admin
from django.urls import path,include
from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.urls import re_path
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home.urls')),
path('blogs/',include('blog.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
app/models.py
from django.db import models
from django.utils.timezone import now
from django.contrib.auth.models import User
# Create your models here.
class BlogPost(models.Model):
blog_id=models.AutoField(primary_key=True)
author=models.CharField(max_length=200)
title=models.CharField(max_length=300)
pub_date=models.DateField()
category=models.CharField(max_length=200,default="Promotional Blogs")
heading1=models.CharField(max_length=300,blank=True)
content1=models.TextField(blank=True)
heading2=models.CharField(max_length=300)
content2=models.TextField()
about=models.TextField()
likes=models.ManyToManyField(User, related_name="blogpost_like")
image=models.ImageField(upload_to="blog/images")
def number_of_likes(self):
return self.likes.count()
def __str__(self):
return self.title + " by " + self.author
app/views.py
def createblog(request):
if request.method=="POST":
author=request.POST.get("author")
title=request.POST.get("title")
today=date.today()
category=request.POST.get("category")
heading=request.POST.get("heading")
body=request.POST.get("body")
about=request.POST.get("about")
pic=request.POST.get("pic")
new_post=BlogPost(author=author,title=title,pub_date=today,category=category,heading2=heading,content2=body,about=about,image=pic)
new_post.save()
messages.success(request,"Your blog is published successfully.")
return redirect("/blogs")
return render(request,"blog/editor.html")
app/editor.html
{% extends 'blog/basic.html' %}
{% block title %}Editor - BlogLikho{% endblock %}
{% block body %}
<div class="container my-4">
<h1 class="mb-4" style="text-align:center;">Blog Editor</h1>
<form method="POST" action="/blogs/createblog">{% csrf_token %}
<div class="row">
<div class="mb-3 col-md-6">
<label for="author" class="form-label" style="font-weight: bold;">Author</label>
<input type="text" class="form-control" id="author" name="author" placeholder="" required>
</div>
<div class="mb-3 col-md-6">
<label for="title" class="form-label" style="font-weight: bold;">Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="Blog title" required>
</div>
</div>
<div class="mb-3">
<label for="category" class="form-label" style="font-weight: bold;">Category</label>
<select class="form-select" aria-label="Default select example">
<option selected>Open this select menu</option>
<option value="Coding Blogs">Coding Blogs</option>
<option value="Sports Blogs">Sports Blogs</option>
<option value="Traveling Blogs">Traveling Blogs</option>
<option value="Educational Blogs">Educational Blogs</option>
<option value="Business Blogs">Business Blogs</option>
<option value="Marketing Blogs">Marketing Blogs</option>
<option value="Technology Blogs">Technology Blogs</option>
<option value="Sales Blogs">Sales Blogs</option>
</select>
<input type="hidden" name="category" value="Coding Blogs">
</div>
<div class="mb-3">
<label for="heading" class="form-label" style="font-weight: bold;">Heading</label>
<input type="text" class="form-control" id="heading" name="heading" placeholder="" required>
</div>
<div class="mb-3">
<label for="body" class="form-label" style="font-weight: bold;">Body</label>
<textarea class="form-control" id="body" name="body" rows="4" required></textarea>
</div>
<div class="mb-3">
<label for="about" class="form-label" style="font-weight: bold;">About you</label>
<textarea class="form-control" id="about" rows="3" name="about" placeholder="Write a bit about yourself" required></textarea>
</div>
<div class="mb-3">
<label for="pic" class="form-label" style="font-weight: bold;">Blog display image</label>
<input class="form-control" type="file" id="pic" name="pic">
</div>
<button type="submit" class="publish btn btn-success">Publish</button>
</form>
</div>
{% endblock %}
app/yourblogs.html
{% extends "blog/basic.html" %}
{% block activeblogs %}active{% endblock %}
{% block title %}Blogs - BlogLikho{% endblock %}
{% load static %}
{% block body %}
<div class="container my-4">
<div class="row mb-2">
{% for blog_item in myblogs %}
<div class="col-md-6">
<div class="row g-0 border rounded overflow-hidden flex-md-row mb-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-success">{{blog_item.author}}</strong>
<h4 class="mb-0">{{blog_item.title | truncatechars:25 }}</h4>
<div class="mb-1 text-muted">{{blog_item.pub_date}}</div>
<p class="card-text mb-auto" style="margin-bottom: revert!important;">{{blog_item.content1 | truncatewords:10}}</p>
<button type="button" class="btn btn-primary" style="width: fit-content;">Continue reading</button>
</div>
<div class="col-auto d-none d-lg-block">
<img src="/media/{{blog_item.image}}" class="bd-placeholder-img" width="250" height="250" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title>
</div>
</div>
</div>
{% endfor %}
{% endblock %}
What might be the problem?? The error which i am getting by clicking on the image url inside the specific BlogPost ImageField is..
404:Page not found
Please suggest me the solution.
I am working on a project for a Django web-based application. In this project, there is a section in which I take info from the user through an HTML form. I added a section "image upload " but it gives a MultiValueDictKeyError error when the user does not upload any file. I tried this but not working for me.
This is error section : error image
This is my addpost.html section. It consists of a form through which, I am taking info.
<form action="{% url 'addpost' %}" method='POST' enctype="multipart/form-data" novalidate>
{% include 'includes/messages.html' %}
{% csrf_token %}
{% if user.is_authenticated %}
<input type="hidden" name="user_id" value="{{user.id}}">
{% else %}
<input type="hidden" name="user_id" value="0">
{% endif %}
<div class="row ">
<div class="tex-center">
<div class="row">
<div class="col-md-6 text-left">
<div class="form-group name">
<input type="text" name="author" class="form-control" placeholder="Author"
{% if user.is_authenticated %} value="{{user.first_name}}" {% endif %} readonly>
</div>
</div>
<div class="col-md-6">
<div class="form-group name">
<input type="text" name="title" class="form-control" placeholder="title" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group name">
<input type="text" name="location" class="form-control" placeholder="location" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group name">
<input type="text" name="short_desc" class="form-control"
placeholder="Write short description" required>
</div>
</div>
<div class="col-md-12">
<div class="form-group message">
<textarea class="form-control" name="full_desc"
placeholder="Write full description"></textarea>
</div>
</div>
<input type="file" id="myFile" name="place_photo" required/>
<div class="col-md-12">
<div class="send-btn text-center">
<input type="submit" class="btn btn-outline-success mr-1" value="Send Post">
Cancel
</div>
</div>
</div>
</div>
</div>
</form>
This is the views.py file where I receive POST data
def add_post(request):
if request.method == "POST":
try:
is_private = request.POST['is_private']
except MultiValueDictKeyError:
is_private = False
author = request.POST['author']
title = request.POST['title']
user_id = request.POST.get('user_id')
location = request.POST['location']
short_desc = request.POST['short_desc']
full_desc = request.POST['full_desc']
place_photo = request.FILES['place_photo']
post = Post(author=author, user_id=user_id, title=title, location=location,
short_desc=short_desc,full_desc=full_desc, place_photo=place_photo)
post.save()
messages.success(request,"Your post uploaded successfully")
return render(request,'community/addpost.html')
This is my models.py file
class Post(models.Model):
author = models.CharField(max_length=100, default=' ')
title = models.CharField(max_length=150)
user_id = models.IntegerField(blank=True)
location = models.CharField(max_length=100, default=' ')
short_desc = models.CharField(max_length=255, default='In less than 250 words')
full_desc = models.TextField()
place_photo = models.ImageField(upload_to='photos/%Y/%m/%d/')
added_date = models.DateTimeField(default=datetime.now,blank=True)
I have created a Customer model in Django and am not able to save the data to the database table and when I click the signup button it just redirects me back to the home page.
I have attached below the customer model and the signup function.
...please help me resolve the issue.
customer.py
from django.db import models
class Customer(models.Model):
first_name= models.CharField(max_length=50)
last_name= models.CharField(max_length=50)
phone=models.CharField(max_length=15)
email=models.EmailField()
password=models.CharField(max_length=500)
def register(self):
self.save()
views.py
def signup(request):
if request.method == 'GET':
return render(request, 'signup.html')
else:
postData=request.POST
first_name=postData.get('firstname')
last_name=postData.get('lastname')
phone=postData.get('phone')
email=postData.get('email')
password=postData.get('password')
print(first_name,last_name,phone,email,password)
customer=Customer(first_name=first_name,last_name=last_name,phone=phone,email=email,password=password)
customer.register()
return HttpResponse("Signup success")
signup.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="p-4 m-4">
<div class="col-lg-5 mx-auto border rounded pt-4">
<h3 class="alert alert-light border rounded" >Create An Account</h3>
<form action="/" method="POST">
{% csrf_token %}
<!--firstname-->
<div class="form-group">
<label for="">First Name</label>
<input type="text" name="firstname" id="" class="form-control form-control-sm" placeholder="Mike">
</div>
<!--lastname-->
<div class="form-group">
<label for="">Last Name</label>
<input type="text" name="lastname" id="" class="form-control form-control-sm" placeholder="Ross">
</div>
<!--phone-->
<div class="form-group">
<label for="">Phone No</label>
<input type="text" name="phone" id="" class="form-control form-control-sm" placeholder="9876543210">
</div>
<!--email-->
<div class="form-group">
<label for="">Email</label>
<input type="email" name="email" id="" class="form-control form-control-sm" placeholder="abc#gmail.com">
</div>
<!--password-->
<div class="form-group">
<label for="">Password</label>
<input type="password" name="password" id="" class="form-control form-control-sm" placeholder="********">
</div>
<div class="form-group">
<input class="btn btn-sm btn-info" type="submit" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
{% endblock %}
Try this change
Model
def register(self):
return self.save()
I have an order information html template to user in multiple pages inside my website using Jekyll
<div class="ccpanel">
{% include order_info.html %}
</div>
In my order_info.html fragment template, I have 5 fields.
<div class="row">
<div class="form-group col-md-4">
<label>First Name</label>
<input class="form-control" placeholder="First Name">
</div>
<div class="form-group col-md-4">
<label>Last Name</label>
<input class="form-control" placeholder="Last Name">
</div>
<div class="form-group col-md-4">
<label>Order ID</label>
<input class="form-control">
</div>
<div class="form-group col-md-4">
<label>Order Description</label>
<input class="form-control">
</div>
<div class="form-group col-md-4">
<label>Order Note</label>
<input class="form-control">
</div>
</div>
I have some pages that need to display all 5 fields in the order_info.html which works fine but I also have some pages that only need to display 3 fields in the order_info.html.
How can I reuse the order_info.html template for all pages? Or do I have to create another template of order info with 3 fields?
I believe you could pass a variable to the template, and do an if condition inside
{% include order_info.html fields=3 %}
Then inside
{% if include.fields == 3 %}
{% endif %}