Using Django registration with a Flat UI template - django

I have created a template design for signup page using Flat UI. Now i want to use Django registration to register a user. I have goggled various resources but they are very complex and using Django inbuilt form to create a form.
I need simple steps that i can follow
signup.html(home/django/mysite/templates)
{% extends "base.html" %}
{% block title %}Signup page{% endblock %}
{% block content %}
<div class="container" style="width:500px">
<h3 style="margin-top:100px">Sign Up</h3>
<hr>
<div class="login-form">
<form action="" method="post">
<div class="control-group span3" style="width:400px" >
<input type="text" value="" placeholder="Enter your name" id="name" style="width:400px;padding-bottom:15px;margin-bottom:10px" >
<i class="input-icon fui-user" for="login-name"></i>
</div>
<div class="control-group span3" style="width:400px" >
<input type="text" value="" placeholder="Your E-mail" id="email" style="width:400px;padding-bottom:15px;margin-bottom:10px" >
<i class="input-icon fui-mail" for="login-name"></i>
</div>
<div class="control-group span3" style="width:400px">
<input type="password" value="" placeholder="Password" id="pass" style="width:400px;padding-bottom:15px;margin-bottom:10px">
<i class="input-icon fui-lock" for="login-pass"></i>
</div>
<div class="control-group span3" style="width:400px">
<input type="password" value="" placeholder="Confirm Password" id="login-pass" style="width:400px;padding-bottom:15px;margin-bottom:10px">
<i class="input-icon fui-lock" for="login-pass"></i>
</div>
<div style="text-align:center">
<a class="btn btn-hg btn-primary btn-wide" href="#">Sign Up</a>
<!--<a class="login-link" href="#">Lost your password ?</a> -->
</div>
</form>
</div><!-- /login-form -->
</div> <!-- /container -->
{% endblock content %}
views.py
def signup(request):
return render(request, 'signup.html')
urls.py
urlpatterns = patterns('',
url(r'^signup/$', views.signup),)
What i should write in views.py or models.py to register a user using django registration.

You can use django registration together with custom html/css through django forms. Here are the steps:
Provided you have included relevant imports, your urls.py looks fine so no changes needed.
Create a forms.py file in the same folder as your views.py and add the following code into forms.py:
from django import forms
class Signup_form(forms.Form):
# CSS styles should not be inline. i've moved your style contents under a 'form-control' class
name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'type':'text', 'placeholder':'Enter your name', 'id':'name', 'name':'name', 'class' : 'form-control'}))
email = forms.EmailField(label="Email address", widget=forms.TextInput(attrs={'type':'text', 'placeholder':'Your E-mail', 'id':'email', 'name':'email', 'class' : 'form-control'}))
pass1 = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'Password', 'id':'pass1', 'name':'pass1', 'class' : 'form-control'}))
pass2 = forms.CharField(max_length = 20, widget=forms.TextInput(attrs={'type':'password', 'placeholder':'Confirm Password', 'id':'pass2', 'name':'pass2', 'class' : 'form-control'}))
3.In your views.py file, you have to link pass the Signup_form to your views layer. Change your views.py file to the following:
from forms import Signup_form
def signup(request):
form = Signup_form()
name = request.POST.get('name','')
email = request.POST.get('email', '')
pass1 = request.POST.get('pass1', '')
pass2 = request.POST.get('pass2', '')
# Do some validations here
user = User.objects.create_user(name, email, pass2)
if user:
user.save()
return render(request, 'signup.html', {'form': form})
4.Now that you have passed your Signup_form object in views layer, you can use django template tags to display them in your signup.html page. Here's how your signup.html could look like:
{% extends "base.html" %}
<link rel="stylesheet" href="{% static 'css/custom.css' %}">
{% block title %}Signup page{% endblock %}
{% block content %}
<div class="container" style="width:500px">
<h3 style="margin-top:100px">Sign Up</h3>
<hr>
<div class="login-form">
<form action="" method="post"> {% csrf_token %}
<div class="control-group span3" style="width:400px" >
{{ form.name.errors }}
{{ form.name }}
<i class="input-icon fui-user" for="login-name"></i>
</div>
<div class="control-group span3" style="width:400px" >
{{ form.email.errors }}
{{ form.email }}
<i class="input-icon fui-mail" for="login-name"></i>
</div>
<div class="control-group span3" style="width:400px">
{{ form.pass1.errors }}
{{ forms.pass2 }}
<i class="input-icon fui-lock" for="login-pass"></i>
</div>
<div class="control-group span3" style="width:400px">
{{ form.pass2.errors }}
{{ forms.pass2 }}
<i class="input-icon fui-lock" for="login-pass"></i>
</div>
<div style="text-align:center">
<input type="submit" class="btn btn-hg btn-primary btn-wide" value="Sign Up">
<!--<a class="login-link" href="#">Lost your password ?</a> -->
</div>
</form>
</div><!-- /login-form -->
</div> <!-- /container -->
{% endblock content %}
5.Since i have earlier moved your CSS styles(in point 2) into a 'form-control' class, now we will place it back by adding your styles in a external custom.css file. Create a custom.css file in the directory static/css/custom.css and add the following into it:
.form-control {
width:400px;
padding-bottom:15px;
margin-bottom:10px;
}

In your Views.py
from django.contrib.auth.models import User
def signup(request):
username = request.POST.get("name","")
password = request.POST.get("pass","")
login_pass = request.POST.get("login-pass","")
email = request.POST.get("email","")
if username != "" or password != "" or login_pass != "":
#do some validations
user = User.objects.create_user(username, email, password)
if user:
user.is_active = True
user.save()
return render(request, 'signup.html')
Above code will register a new user into system
If you would have use Django forms then all the validations will be done by Django.
Note: Please add name attribute to your HTML fields required for getting vaLues in request.POST. Also point your HTMl form action to "signup" view

Related

Django Custom Password Reset Form widget not working

Stumped as to why my template isn't showing my custom password reset form.
Here is code:
forms.py
class CustomPasswordResetForm(PasswordResetForm):
def __init__(self, *args, **kwargs):
super(CustomPasswordResetForm, self).__init__(*args, **kwargs)
email = forms.EmailField(
label='', widget=forms.EmailInput(attrs={
'placeholder': 'placetest#test.com',
'class': 'form-field', }))
Views.py
class PasswordResetView(auth_views.PasswordResetView):
form_class = CustomPasswordResetForm
template_name = 'password_reset.html'
urls.py
urlpatterns = [
path('login/', LoginView.as_view(), name='login'),
path('password_reset', PasswordResetView.as_view(), name='password_reset'),
path('login_success/', login_success, name='login_success'),]
Template
{% load static %}
{% block title %}Home{% endblock %}
{% block content %}
<div class="form-container" id="pwrest">
<div class="form-title">
<h2>Enter your email address</h2>
</div>
<form method="POST">
{% csrf_token %}
<p>email</p>
<div>{{ form.email }}</div>
<input class="submit-button" type="submit" value="Send me instructions!">
</form>
</div>
</div>
{% endblock %}
CSS
.form-field{
width: 300px;
height:30px;
margin-bottom:5px;
margin-top:5px;
border:none;
border-radius: 5px;
background-color:whitesmoke;
}
Rendered HTML
<div class="form-title">
<h2>Enter your email address</h2>
</div>
<form method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="TOKEN">
<p>email</p>
<div><input type="email" name="email" autocomplete="email" maxlength="254" required id="id_email"></div>
<input class="submit-button" type="submit" value="Send me instructions!">
</form>
</div>
</div>
When I view the template in my browser, I am seeing the form field as the default, its almost like its just not recognizing the CSS class.
I'm not sure what I've missed.
.

How to make a success ulr for my contact page

I want the client to be redirected to another HTML page as a successfully sent message in my contact form. But I don't know exactly what is wrong here. I can't redirect to my success page but the form works.
#my main app's url.py
urlpatterns = [
path('^contact/', include('contactus.urls')),
]
#my contact app url.py
from __future__ import unicode_literals
from django.conf.urls import url
from django.views.generic import TemplateView
from .views import ContactUsView
urlpatterns = [
url(r'^$', ContactUsView.as_view(), {}, 'contactus'),
url(r'^success/$', TemplateView.as_view(),
{}, 'contactus-success'),
]
#contact app view.py
from __future__ import unicode_literals
from django.conf import settings
from django.core.mail import EmailMessage
from django.template import loader
from django.views.generic.edit import FormView
from contactus.forms import ContactUsForm
class ContactUsView(FormView):
template_name = 'contactus/contact.html'
email_template_name = 'contactus/contact_notification_email.txt'
form_class = ContactUsForm
success_url = "/contact/success/"
subject = "Contact Us Request"
def get_initial(self):
initial = super(ContactUsView, self).get_initial()
if not self.request.user.is_anonymous:
initial['name'] = self.request.user.get_full_name()
initial['email'] = self.request.user.email
return initial
def form_valid(self, form):
form_data = form.cleaned_data
if not self.request.user.is_anonymous:
form_data['username'] = self.request.user.username
# POST to the support email
sender = settings.SERVER_EMAIL
recipients = (getattr(settings, 'CONTACT_US_EMAIL'),)
reply_to = form_data.get('email') or sender
tmpl = loader.get_template(self.email_template_name)
email = EmailMessage(
self.subject,
tmpl.render(form_data),
sender,
recipients,
reply_to=[reply_to],
)
email.send()
return super(ContactUsView, self).form_valid(form)
#contact app forms.py
from __future__ import unicode_literals
from django import forms
class ContactUsForm(forms.Form):
name = forms.CharField(required=True, max_length=512)
email = forms.EmailField(required=True)
subject = forms.CharField(required=True, max_length=512)
description = forms.CharField(
widget=forms.Textarea(attrs={'class': "form-control"}),
required=True)
def clean(self):
cleaned_data = super(ContactUsForm, self).clean()
return cleaned_data
#Contact.html
{% extends 'website/base.html' %}
{% load static %}
{% block main %}
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Contact</h2>
<ol>
<li>Home</li>
<li>Contact</li>
</ol>
</div>
</div>
</section><!-- End Breadcrumbs -->
<section id="contact" class="contact">
<div class="container">
<div class="row mt-5 justify-content-center" data-aos="fade-up">
<div class="col-lg-10">
<form action="." method="post" role="form" class="php-email-form">{% csrf_token %}
<div class="row">
<div class="col-md-6 form-group {% if form.name.errors %}has-error{% endif %}">
<input type="text" name="name" class="form-control"
data-rule="minlen:4" data-msg="Please enter at least 4 chars"
id="id_name" placeholder="Your Name"
value="{% if form.data.name %}{{form.data.name}}{% else%} {{form.initial.name}}{% endif %}" />
<div class="validate"></div>
</div>
<div class="col-md-6 form-group mt-3 mt-md-0 {% if form.email.errors %}has-error{% endif %}">
<input class="form-control" type="email" name="email"
id="email" data-rule="email" data-msg="Please enter a valid email"
id="id_email" placeholder="Your Email"
value="{% if form.data.email %}{{form.data.email}}{% else %}{{form.initial.email}}{% endif %}" />
<div class="validate"></div>
</div>
</div>
<div class="form-group mt-3 {% if form.subject.errors %}has-error{% endif %}">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject"
data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validate"></div>
<div class="validate"></div>
</div>
<div class="form-group mt-3 {% if form.description.errors %}has-error{% endif %}">
<textarea class="form-control" name="description" rows="5" data-rule="required"
data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validate"></div>
</div>
<div class="mb-3">
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact Section -->
{% endblock %}
The error could be from the view.
try:
success_url = "/success/"
or you could leave your view like that and change the url in urls.py to:
url(r'^contact/success/$',......),

Django - Account Settings BooleanField

I reviewed most of the posts here related to Django and BooleanField update, but I couldn't find any related to my problem solution.
I have a custom user models.py:
# Own custom user model
class Account(AbstractBaseUser):
username = models.CharField(max_length=30, unique=True)
guardianSource = models.BooleanField(default=True)
bbcSource = models.BooleanField(default=False)
independentSource = models.BooleanField(default=False)
categoryCoronaVirus = models.BooleanField(default=False)
categoryPolitics = models.BooleanField(default=False)
categorySport = models.BooleanField(default=False)
When I register a user it seems to register in the database all correct values for the checkboxes. The problem is when I want to edit user information I cannot see if a checkbox was checked on registering or not, it displays the checkboxes itself, but they are all empty (False). However, it correctly requests the username and displays it so I can edit it, but all the checkboxes are unchecked.
views.py:
def account_view(request):
if not request.user.is_authenticated:
return redirect('login')
context = {}
if request.POST:
form = AccountUpdateForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
context['success_message'] = "Updated"
else: # Display the saved user details from database
form = AccountUpdateForm(
initial = {
'username':request.user.username,
"guardianSource": request.user.guardianSource,
"bbcSource": request.user.bbcSource,
"independentSource": request.user.independentSource,
"categoryCoronaVirus": request.user.categoryCoronaVirus,
"categoryPolitics": request.user.categoryPolitics,
"categorySport": request.user.categorySport,
})
context['account_form'] = form
return render(request, 'accounts/account.html', context)
account html:
{% extends 'base.html' %}
{% block content %}
<form class="form-signin" method="POST">{% csrf_token %}
<h1 class="h3 mb-3 font-weight-normal">Account Settings</h1>
<label for="username" class="sr-only">Username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Username" value="{{account_form.initial.username}}">
<br>
<div class="form-control">
<p><b>Please choose news sources!</b></p>
<label for="guardianSource" >The Guardian</label>
<input type="checkbox" name="guardianSource" id="guardianSource" value="{{account_form.initial.guardianSource}}" >
<br>
<label for="bbcSource" >BBC News</label>
<input type="checkbox" name="bbcSource" id="bbcSource" value="{{account_form.initial.bbcSource}}" >
<br>
<label for="independentSource" >The Independent</label>
<input type="checkbox" name="independentSource" id="independentSource" value="{{account_form.initial.independentSource}}" >
</div>
<br>
<div class="form-control">
<p><b>Please choose news category!</b></p>
<label for="categoryCoronaVirus" >The Guardian</label>
<input type="checkbox" name="categoryCoronaVirus" id="categoryCoronaVirus" value="{{account_form.initial.categoryCoronaVirus}}" >
<br>
<label for="categoryPolitics" >BBC News</label>
<input type="checkbox" name="categoryPolitics" id="categoryPolitics" value="{{account_form.initial.categoryPolitics}}" >
<br>
<label for="categorySport" >The Independent</label>
<input type="checkbox" name="categorySport" id="categorySport" value="{{account_form.initial.categorySport}}">
</div>
{% for field in registration_form %}
<p>
{% for error in field.errors %}
<p sttle="color:red"> {{ error }}</p>
{% endfor %}
</p>
{% endfor %}
{% if registration_form.non_field_errors %}
<div style="color:red;">
<p>{{registration_form.non_field_errors}}</p>
</div>
{% endif %}
{% for field in account_form %}
<p>
{% for error in field.errors %}
<p sttle="color:red"> {{ error }}</p>
{% endfor %}
</p>
{% endfor %}
{% if account_form.non_field_errors %}
<div style="color:red;">
<p>{{account_form.non_field_errors}}</p>
</div>
{% endif %}
{% if success_message %}
<p style = "color: green; text-align:center;">{{success_message}}</p>
{% endif %}
<h6 class="text-muted">
If you don't choose a source and category it will automatically assign the ones that are checked!<br>
NOTE: You <b>MUST</b> select at least 1 choice for each!!!
</h6>
<button class="btn btn-lg btn-primary btn-block" type="submit">Save Changes</button>
</form>
<div class="d-flex flex-column">
<a class="m-auto" href="{% url 'password_change' %}">Change password</a>
</div>
{% endblock content %}
Thanks in advance and I am sorry if my post is duplicated.
EDIT:
The issue was found.
In the account.html file the values for each input type checkbox were wrong.
What I changed from:
<input type="checkbox" name="guardianSource" id="guardianSource" value="{{account_form.initial.guardianSource}}" >
To:
<input type="checkbox" name="guardianSource" id="guardianSource" value="{{account_form.guardianSource}}" >
For all the input values after the username input the initial had to be removed
You don't need to pass initial when you are initializing a ModelForm and have the instance
else: # Display the saved user details from database
form = AccountUpdateForm(instance=request.user)
You can use the field directly in the template and it will be rendered with the correct values, you don't need to construct the html for the input yourself
<label for="{{ account_form.guardianSource.id_for_label }}">The Guardian</label>
{{ account_form.guardianSource }}
The docs have a section on how to render fields manually

Django contact form doesnt submit

I am trying to set up a contact form. I have implemented the Django-crispy-forms and now my form is not submitted (I don't have any errors).
I've added action="" to my form in my template without any success.
forms.py
class ContactForm(forms.Form):
name = forms.CharField(max_length=100, help_text='Enter your name or username')
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'cols': 40}), help_text='Example: I forgot my password!')
views.py
def contact_us(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
sender_name = form.cleaned_data['name']
sender_email = form.cleaned_data['email']
message = "From {0}:\n\n{1}".format(sender_name, form.cleaned_data['message'])
send_mail('PLM_Tool contact', message, sender_email, ['myadress#gmail.com'])
return redirect('home:index')
else:
form = ContactForm()
return render(request, 'accounts/contact.html', {'form': form})
urls.py
app_name = 'accounts'
urlpatterns = [path('contact/', views.contact_us, name='contact'),]
contact.html
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block main %}
<form method="post" action="">
{% csrf_token %}
<div class="row">
<div class="col-6">
{{ form.name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.email|as_crispy_field }}
</div>
<div class="col-6">
{{ form.message|as_crispy_field }}
</div>
</div>
</form>
<button type="submit" class="btn btn-success">Send</button>
Cancel
<br><br>
{% endblock %}
Here is the problem, and do not give action to form
crispy forms create the field not the button.
<form method="post">
{% csrf_token %}
<div class="row">
<div class="col-6">
{{ form.name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.email|as_crispy_field }}
</div>
<div class="col-6">
{{ form.message|as_crispy_field }}
</div>
</div>
<button type="submit" class="btn btn-success">Send</button>
</form>
just add the button inside the form

Django edit view does not show

I am new to Django.
My app allows a user to create a project by providing a title and a body content, delete it and update it.
Now, I am creating and edit/update view for my app but it is not showing up in my html page.
urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path('allprojects', views.allprojects, name='allprojects'),
path('createproject', views.createproject, name='createproject'),
path('<int:project_id>', views.projectdetail, name='projectdetail'),
path('<int:project_id>/editproject', views.editproject, name='editproject'),
]
projects/views.py
#login_required
def editproject(request, project_id):
if request.method == 'POST':
if request.POST['title'] and request.POST['content']:
project = get_object_or_404(Project, pk=project_id)
project.title = request.POST['title']
project.content = request.POST['content']
project.developer = request.user
project.save()
return redirect('/projects/' + str(project.id))
else:
return render(request, 'projects/editproject.html', {'error':'All fields are required.'})
else:
return render(request, 'projects/allprojects.html')
projects/templates/projects/editproject.html
{% extends 'base.html' %}
{% block title %}Edit Project{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="mt-4 offset-md-3 col-md-6">
<h2>Create a new project</h2>
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlTextarea1">Title of the project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="1" name="title"></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Brief description of this project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
{% endblock %}
PROBLEM
When I go to a urls such as http://localhost:8000/projects/2/editproject there is not the update form I want, the page exists but it contains nothing
You have to pass the project = get_object_or_404(Project, pk=project_id) in the dictionary in order to get the value in template.
Change your view like this:
#login_required
def editproject(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == 'POST':
if request.POST['title'] and request.POST['content']:
project.title = request.POST['title']
project.content = request.POST['content']
project.developer = request.user
project.save()
return redirect('/projects/',project.project_id)
return render(request, 'projects/editproject.html', {'error':'All fields are required.','project':project})
And in the template you can get value like this :
Also in your template change method=put to method=post
and also you need to provide action if you want to update.
<div class="form-group">
<label for="exampleFormControlTextarea1">Title of the project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="1" name="title">
{{project.title}}
</textarea>
</div>