i am still learning django. trying to create a comment form in my blogdetail view.. i am following a tutorial from youtube..
https://www.youtube.com/watch?v=An4hW4TjKhE&t=40s
this tutorial has form which takes users from django users .. but in my form i want user to enter his name ,email and content..
following this tutorial i made form model but i have no idea how to get data from my own html form by this..
i have reached to a stage where in admin i can add comments and display them in my html file but now i am getting an error..
name 'post' is not defined
my files are..
forms.py
from django import forms
from.models import Comment
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('content', 'email', 'name' ,)
models.py
class BlogPost(models.Model):
title = models.CharField(max_length=500)
writer = models.CharField(max_length=150,default='my dept')
category =models.CharField(max_length=150)
image = models.ImageField(upload_to='images')
post = models.TextField(max_length=2000)
Date = models.DateField( default=datetime.date.today)
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(BlogPost , on_delete=models.CASCADE)
name = models.CharField (max_length = 150)
email = models.CharField (max_length = 150)
content = models.TextField ()
def __str__(self):
return self.email
views.py
def detailview(request, id=None):
blg = get_object_or_404(BlogPost, id=id)
comments = Comment.objects.filter( post=blg).order_by('-id')
if request.method == 'POST':
comment_form = CommentForm(request.POST or None )
if comment_form.is_valid():
content = request.POST.get('content')
Comment.objects.create(post= post , content=content)
comment_form.save()
else:
comment_form = CommentForm()
context = {'blg': blg,
'comments': comments,
'comment_form' : comment_form,
}
return render(request, 'blog/blogdetail.html', context)
blogdetail.html
<div id="respond" class="clearfix">
<div class="title-box">
<h3>Leave a <strong>Comment</strong></h3>
</div><!-- end title-box -->
<div class="row">
<!-- start Comment Form -->
<form class="clearfix" action="#" method="post" id="commentform">
{% csrf_token %}
<p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>
<div class="form-group">
<div class="col-sm-12">
<label>Comment<span class="required">*</span></label>
<textarea name="comment" cols="58" rows="7" tabindex="4" class="form-control" required >{{comment.content}}</textarea>
</div>
</div><!-- end form-group -->
<div class="form-group">
<div class="col-sm-6">
<label>Name<span class="required">*</span></label>
<input name="author" id="author" value="" size="22" tabindex="1" class="form-control" type="text" required >{{comment.name}}</div>
<div class="col-sm-6">
<label>Email<span class="required">*</span></label>
<input name="email" id="email" value="" size="22" tabindex="2" class="form-control" type="text" required >{{comment.email}}</div>
</div><!-- end form-group -->
<div class="form-group">
<div class="col-sm-12">
<button name="submit" type="submit" id="submit-button" tabindex="5" value="Submit" class="btn btn-shutter-out-horizontal btn-lg">Submit Comment</button>
</div>
</div><!-- end form-group -->
</form>
<!-- end Comment Form -->
</div>
</div><!-- #respond end -->
it actually looks like this
[![comment form][1]][1]
i want to post name email and comment from front end .. wasted almost 2 days without no success.. please help..
[1]: https://i.stack.imgur.com/HjDZ6.png
since you are passing the data manually from html rewrite this function
if comment_form.is_valid():
content = request.POST.get('content', '')
email = request.POST.get('email', '')
name = request.POST.get('author', '')
comment =Comment.objects.create(post= blg, email=email, name=name, content=content)
return redirect('posts-detail', pk=comment.id )<this can be how you like it>
what you are missing is getting the missing fields in html they are taken from name attribute in the input
if comment_form.is_valid():
content = request.POST.get('content')
Comment.objects.create(post= blg , content=content) # YOU HAVE ADDED post= post even post variable you are not define
comment_form.save()
Related
Basically I have set up a form to create organizations. When I hit the Save button, it simply renders the page again - the POST is not working.
See my code below:
models.py
from django.db import models
from accounts.models import User
from datetime import datetime, date
#// ------------ FUNCTIONS -------------//
# Generate Organisation IDs for each organisation
def org_id_generate():
last_org = Organization.objects.all().order_by('org_id').last()
if not last_org:
return 'ORG_001'
else:
last_org_id = last_org.org_id
number_in_id = int(last_org_id[4:7])
new_number_in_id = number_in_id + 1
new_org_id = 'ORG_' + str(new_number_in_id).zfill(3)
return new_org_id
#// ------------ MODELS -------------//
class Organization(models.Model):
org_id = models.CharField(primary_key=True, max_length=7, default=org_id_generate, editable=False)
organization_code = models.CharField(max_length=20)
company_name = models.CharField(verbose_name="Company Name", max_length=60)
legal_name = models.CharField(verbose_name="Legal Name", max_length=100)
industry_distribution = models.BooleanField(verbose_name="Distribution", default=False)
industry_education = models.BooleanField(verbose_name="Education", default=False)
industry_healthcare = models.BooleanField(verbose_name="Healthcare", default=False)
industry_manufacturing = models.BooleanField(verbose_name="Manufacturing", default=False)
industry_retail = models.BooleanField(verbose_name="Retail", default=False)
industry_services = models.BooleanField(verbose_name="Services", default=False)
business_registration_no = models.CharField(verbose_name="Business Registration Number", max_length=15, blank=True)
vat_registration_no = models.CharField(verbose_name="VAT Registration Number", max_length=15, blank=True)
created_date = models.DateTimeField(default=datetime.now)
created_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="Created_By", verbose_name="Created By")
effective_start_date = models.DateField(auto_now_add=False)
effective_end_date = models.DateField(auto_now_add=False, blank=True, null=True)
update_date = models.DateTimeField(default=datetime.now)
last_updated_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="Last_Updated_By", verbose_name="Last Updated By")
def __str__(self):
return self.company_name
forms.py
from django import forms
from organizations.models import Organization
class OrganizationAddForm(forms.ModelForm):
class Meta:
model = Organization
exclude = ['created_date', 'update_date', ]
views.py
from django.shortcuts import get_object_or_404, render, redirect
from organizations.models import Organization
from forms import OrganizationAddForm
from accounts.models import User
from django.contrib.auth.decorators import login_required
#login_required()
def settings(request):
return render(request, 'settings/settings.html')
#login_required()
def organizations_settings(request):
orgs = Organization.objects.all()
context = {
'orgs': orgs,
}
return render(request, 'settings/settings_organizations.html', context)
#login_required
def organization_add(request):
if request.method == 'POST':
user_email = request.user.email
form = OrganizationAddForm(request.POST)
if form.is_valid():
form.organization_code = form.cleaned_data['organization_code']
form.company_name = form.cleaned_data['company_name']
form.legal_name = form.cleaned_data['legal_name']
form.business_registration_no = form.cleaned_data['brn']
form.vat_registration_no = form.cleaned_data['vat']
form.industry_distribution = form.cleaned_data['industry_distribution']
form.industry_education = form.cleaned_data['industry_education']
form.industry_healthcare = form.cleaned_data['industry_healthcare']
form.industry_manufacturing = form.cleaned_data['industry_manufacturing']
form.industry_retail = forms.cleaned_data['industry_retail']
form.industry_services = form.cleaned_data['industry_services']
form.effective_start_date = form.cleaned_data['effective_start_date']
form.effective_end_date = form.cleaned_data['effective_end_date']
form.created_by = form.cleaned_data[user_email]
form.last_updated_by = form.cleaned_data[user_email]
form.save()
return redirect('organizations_settings')
else:
form = OrganizationAddForm()
return render(request, 'settings/add_organization.html', {'form': form})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.settings, name="settings"),
path('organizations/', views.organizations_settings, name='organizations_settings'),
path('organization_create', views.organization_create, name="organization_create"),
path('organizations/add/', views.organization_add, name="organization_add"),
]
Page Source Code
<!-- Add Organization Form -->
<div class="container form-style">
<i class="fas fa-arrow-left"></i> Back
<div class="form-header">
<h3>Add Organization</h3>
</div>
<form action="{% url 'organization_add' %}" method="POST">
{% csrf_token %}
<div class="container">
<!-- Row 1 -->
<div class="row">
<div class="col-md-4">
<label for="organization_code">Organization Code<span class="star-red">*</span></label>
<input type="text" name="organization_code" class="form-control" required>
</div>
<div class="col-md-4">
<label for="company_name">Organization Name<span class="star-red">*</span></label>
<input type="text" name="company_name" class="form-control" required>
</div>
<div class="col-md-4">
<label for="legal_name">Legal Name<span class="star-red">*</span></label>
<input type="text" name="legal_name" class="form-control" required>
</div>
</div>
<!-- Row 2 -->
<div class="row mt-4">
<!-- <div class="col-md-4">
<label for="industry">Industry<span class="star-red">*</span></label>
<select name="industry" class="selectpicker">
<option value="distribution">Distribution</option>
<option value="education">Education</option>
<option value="healthcare">Healthcare</option>
<option value="manufacturing">Manufacturing</option>
<option value="retail">Retail</option>
<option value="services">Services</option>
</select>
</div> -->
<div class="col-md-6">
<label for="brn">Business Registration No.</label>
<input type="text" name="brn" class="form-control">
</div>
<div class="col-md-6">
<label for="vat">VAT Registration No.</label>
<input type="text" name="vat" class="form-control">
</div>
</div>
<!-- Row 3 -->
<h5 class="mt-4">Industry</h5>
<div class="row">
<div class="col-md-4">
<div class="form-check">
<input type="checkbox" name="industry_distribution" class="form-check-input">
<label for="industry_distribution" class="form-check-label">Distribution</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input type="checkbox" name="industry_education" class="form-check-input">
<label for="industry_education" class="form-check-label">Education</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input type="checkbox" name="industry_healthcare" class="form-check-input">
<label for="industry_healthcare" class="form-check-label">Healthcare</label>
</div>
</div>
</div>
<div class="row mt-2">
<div class="col-md-4">
<div class="form-check">
<input type="checkbox" name="industry_manufacturing" class="form-check-input">
<label for="industry_manufacturing" class="form-check-label">Manufacturing</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input type="checkbox" name="industry_retail" class="form-check-input">
<label for="industry_retail" class="form-check-label">Retail</label>
</div>
</div>
<div class="col-md-4">
<div class="form-check">
<input type="checkbox" name="industry_services" class="form-check-input">
<label for="industry_services" class="form-check-label">Services</label>
</div>
</div>
</div>
<!-- Row 4 -->
<div class="row mt-4">
<div class="col-md-6">
<label for="effective_start_date">Effective Start Date<span class="star-red">*</span></label>
<input type="date" name="effective_start_date" class="form-control" required>
</div>
<div class="col-md-6">
<label for="effective_end_date">Effective End Date:</label>
<input type="date" name="effective_end_date" class="form-control">
</div>
</div>
<!-- Hidden Input - User -->
<input type="hidden" name="user_email" />
<div class="float-right mt-3">
<button type="submit" class="btn btn-custom save">Save and Close</button>
</div>
</div>
</form>
</div>
Any help would be greatly appreciated. If you need any additional info, let me know and I will edit the post.
I did not find any code to show the errors in html.
According to the function in views, if the form is not valid, then it renders the page with the form.
Try to add {{form.errors}} to you the html file to see if it has errors?
I managed to solve it.
views.py
#login_required
def organization_add(request):
if request.method == 'POST':
form = OrganizationAddForm(request.POST)
if form.is_valid():
form.organization_code = form.cleaned_data['organization_code']
form.company_name = form.cleaned_data['company_name']
form.legal_name = form.cleaned_data['legal_name']
form.business_registration_no = form.cleaned_data['business_registration_no']
form.vat_registration_no = form.cleaned_data['vat_registration_no']
form.industry_distribution = form.cleaned_data['industry_distribution']
form.industry_education = form.cleaned_data['industry_education']
form.industry_healthcare = form.cleaned_data['industry_healthcare']
form.industry_manufacturing = form.cleaned_data['industry_manufacturing']
form.industry_retail = form.cleaned_data['industry_retail']
form.industry_services = form.cleaned_data['industry_services']
form.effective_start_date = form.cleaned_data['effective_start_date']
form.effective_end_date = form.cleaned_data['effective_end_date']
org = form.save(commit=False)
org.created_by = request.user
org.last_updated_by = request.user
org.save()
return redirect('organizations_settings')
else:
form = OrganizationAddForm()
return render(request, 'settings/add_organization.html', {'form': form})
The issue was that it was not able to capture the user email for the Created By and Last Updated By fields.
This is resolved by using:
org = form.save(commit=False)
org.created_by = request.user
org.last_updated_by = request.user
Note that the following two posts helped me:
Using request.user with Django ModelForm
Cannot assign "42": "Event.user_id" must be a "User" instance
I've been lurking for an answer for quite some time now, but I haven't found solution for my problems.
I made custom template for my form and now, when I try to submit form I get this under choice field: Select a valid choice. is not one of the available choices.
I believe that problems is because I am not passing instance of organization but id. I tried {{ form.instance.organization }} and then I get None where should be choice field
views.py:
class AddNewView(generic.View):
formClass = AddNewForm
template_name = 'myapp/subscription_form.html'
def get(self, request):
groups = self.request.user.groups.values_list('id', flat=True).first()
form = self.formClass(groups, None)
return render(request, self.template_name, {'form': form})
def post(self, request):
groups = self.request.user.groups.values_list('id', flat=True).first()
form = self.formClass(groups, request.POST)
if form.is_valid():
subscription = form.save(commit=False)
organization = request.user.groups.values_list('id', flat=True).first()
input_user = self.request.user
stuff = form.cleaned_data['stuff']
description = form.cleaned_data['description']
subscription.save()
return render(request, self.template_name, {'form': form})
forms.py:
class AddNewForm(forms.ModelForm):
def __init__(self, groups,*args,**kwargs):
super (AddNewView, self ).__init__(*args,**kwargs)
self.fields['organization'].queryset = Organization.objects.filter(group=groups)
class Meta:
model = Subscription
fields = [
'organization',
'stuff',
'description',
]
models.py:
class Organization(models.Model):
d_number = models.CharField(max_length=25)
city = models.CharField(max_length=100)
group = models.ManyToManyField(Group, help_text="groups")
class Subscription(models.Model):
organization = models.ForeignKey(Group, help_text="which organization")
input_user = models.CharField(max_length=150)
input_date = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=1000, null=True, blank=True, help_text='Description')
stuff = models.CharField(max_length=100)
template:
<form action="" method="post">
{% csrf_token %}
<!-- Left Inputs -->
<div class="col-xs-6 wow animated slideInLeft" data-wow-delay=".5s">
<!-- Organization -->
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.organization.errors }}
<label>Organization:</label>
{{ form.organization }}
</div>
<!-- stuff -->
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.stuff.errors }}
<input type="text" name="stuff" id="id_stuff" required placeholder="stuff" class="form"/>
</div>
</div><!-- End Left Inputs -->
<!-- Right Inputs -->
<div class="col-xs-6 wow animated slideInRight" data-wow-delay=".5s">
<!-- description -->
{{ form.non_field_errors }}
<div class="fieldWrapper">
<textarea name="description" id="id_description" class="form textarea" placeholder="description"></textarea>
</div>
</div><!-- End Right Inputs -->
<div class="relative fullwidth col-xs-12">
<!-- Send Button -->
<button type="submit" class="form-btn semibold">Vnesi</button>
</div><!-- End Bottom Submit -->
</form>
I am trying to create a ModelForm that links to an external database, and when you submit the form the external database gets updated. The problem comes when I check the validity of the form, it is invalid.
I have done some researching into this and found the most common problem was that the form is not bound, but when I use print(form.non_field_errors) I get:
<bound method BaseForm.non_field_errors of <EmailForm bound=True, valid=False, fields=(subject;body;name;altsubject;utm_source;utm_content;utm_campaign)>
models.py:
class MarketingEmails(models.Model):
messageid = models.AutoField(db_column='column1', primary_key=True)
subject = models.CharField(db_column='column2', max_length=2000)
body = models.TextField(db_column='column3') #using a text field as there is no maximum length
name = models.CharField(db_column='column4', max_length=25)
altsubject = models.CharField(db_column='column5', max_length=2000)
utm_source = models.CharField(db_column='column6', max_length=25)
utm_content = models.CharField(db_column='column7', max_length=25)
utm_campaign = models.CharField(db_column='column8', max_length=25)
class Meta:
managed = False
db_table = ''
forms.py:
class EmailForm(forms.ModelForm):
class Meta:
model = MarketingEmails
fields = ['messageid','subject','body','name','altsubject','utm_source','utm_content','utm_campaign']
views.py:
def emailinfo(request, pk):
if request.session.has_key('shortname'):
shortname = request.session['shortname']
rows = get_object_or_404(MarketingEmails, pk=pk)
if request.method == 'POST':
form = EmailForm(request.POST)
print(form.errors)
print(form.non_field_errors)
if form.is_valid():
form.save()
print("form is valid")
return redirect('marketingemails:emailinfo', pk = rows.messageid)
return render(request, 'marketingemails/emailinfo.html',{'shortname': shortname, 'rows': rows})
else:
return HttpResponseRedirect(reverse('common:login'))
urls.py:
app_name = 'marketingemails'
urlpatterns = [
url(r'^marketing/emails/(?P<pk>[0-9]+)/$', marketingviews.emailinfo, name='emailinfo'),
]
html:
<form method="POST" class="post-form" action ="">
{% csrf_token %}
<label for="exampleTextarea">Name</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.name }}</textarea>
<label for="exampleTextarea">Subject</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.subject }}</textarea>
<label for="exampleTextarea">Alternative Subject</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.altsubject }}</textarea>
<label for="exampleTextarea">Body</label>
<div class="ibox-content no-padding">
<div class="summernote">
{{ rows.body }}
</div>
</div>
<label for="exampleTextarea">utm_source</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.utm_source }}</textarea>
<label for="exampleTextarea">utm_content</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.utm_content }}</textarea>
<label for="exampleTextarea">utm_campaign</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.utm_campaign }}</textarea>
<button type="submit" class="save btn btn-default">Save</button>
</form>
Your HTML form doesn't name the fields so the form can't get them. You want to use the form for rendering too : https://docs.djangoproject.com/en/1.11/topics/forms/#working-with-form-templates
New to Django and this is my first web application.
I'm having trouble with django's ModelForm feature and I wanted to know:
How do I modify my code so that I can create an instance of ModelForm, and specifically, how can I extract the form data to upload to the backend? I will need to reference this instance at a later time to re-populate the same data in an update_profile view but the updation can only happen once the user is logged in (after signup and profile creation).
For the editing section, do I use pk=some_record.pk? Very confused, any help is appreciated.
The model I'm working with CustomerDetail has a foreign key field customer which references the Customer model:
class CustomerDetail(models.Model):
phone_regex = RegexValidator(regex = r'^\d{10}$', message = "Invalid format! E.g. 4088385778")
date_regex = RegexValidator(regex = r'^(\d{2})[/.-](\d{2})[/.-](\d{2})$', message = "Invalid format! E.g. 05/16/91")
customer = models.OneToOneField(Customer,
on_delete=models.CASCADE,
primary_key=True,)
address = models.CharField(max_length=100)
date_of_birth = models.CharField(validators = [date_regex], max_length = 10, blank = True)
company = models.CharField(max_length=30)
home_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True)
work_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True)
def __str__(self):
return str(self.customer)
Here is a snippet of views.py:
def create_profile(request):
if request.POST:
address = request.POST['address']
date_of_birth = request.POST['date_of_birth']
company = request.POST['company']
home_phone = request.POST['home_phone']
work_phone = request.POST['work_phone']
custprofdata = CustomerDetail(address = address, date_of_birth = date_of_birth, company = company, home_phone = home_phone, work_phone = work_phone)
custprofdata.save()
output = {'address': address, 'dateofbirth': date_of_birth, 'company': company, 'homephone': home_phone, 'workphone': work_phone}
return render(request, 'newuser/profile_created.html', output)
else:
return redirect(create_profile)
And here is a snippet of the form part of the respective create_profile.html:
<form action = "{% url 'create_profile' %}" class="create_profile" role="form" method = "post">
{% csrf_token %}
<div class="form-group">
<label for="address" class="col-md-3 control-label">Address</label>
<div class="col-md-9">
<input type="text" class="form-control" name="address" placeholder="777 Park St" />
</div>
</div>
<div class="form-group">
<label for="date-of-birth" class="col-md-3 control-label">Date Of Birth</label>
<div class="col-md-9">
<input type="text" class="form-control" name="date_of_birth" placeholder="09/12/82" />
</div>
</div>
<div class="form-group">
<label for="company" class="col-md-3 control-label">Company</label>
<div class="col-md-9">
<input type="text" class="form-control" name="company" placeholder="Oracle">
</div>
</div>
<div class="form-group">
<label for="home-phone" class="col-md-3 control-label">Home Phone</label>
<div class="col-md-9">
<input type="text" class="form-control" name="home_phone" placeholder="4082992788">
</div>
</div>
<div class="form-group">
<label for="work-phone" class="col-md-3 control-label">Work Phone</label>
<div class="col-md-9">
<input type="text" class="form-control" name="work_phone" placeholder="6690039955">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<button type = "create" class="btn btn-success" form = "create_profile"> Submit </button>
</div>
</div>
</form>
Implementing a basic ModelForm is just a matter of the following:
from django.forms import ModelForm
from .models import CustomerDetail
class CustomerDetailForm(ModelForm):
class Meta:
model = CustomerDetail
fields = ['address', 'date_of_birth', 'company', 'home_phone', 'work_phone',]
https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#a-full-example
But I suggest you also switch to using a Class Based View (CBV) - the CreateView will do the same as your existing view with much less code, with an implicit ModelForm (which you can customise by providing your own ModelForm class with form_class = YourFormClass if you want).
https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-editing/#createview
https://ccbv.co.uk/projects/Django/1.10/django.views.generic.edit/CreateView/
After creating CustomerDetailForm as #John Carter said, you might want to change your view.py to the following
def create_profile(request):
if request.POST:
form = CustomerDetailForm(request.POST)
if form.is_valid():
## save data in database ##
return render(request, 'newuser/profile_created.html', {form:form})
else:
return redirect(create_profile)
I have a very simple thing I want to do, but for some reason I haven't found the solution yet.
I have a form in HTML
<form id="user_form" method="POST" action="/ProjectName/home/">
{% csrf_token %}
<div class="registerLabel">
Full name:
</div>
<div class="registerTextLabel">
<input type="text" id="registerFullName" class="registerTextDetails">
</div>
<div class="registerLabel">
Username:
</div>
<div class="registerTextLabel">
<input type="text" id="registerUsername" class="registerTextDetails">
</div>
<div id="registerButtonDiv">
<button class="registerButtons" id="cancelRegisterButton">Cancel</button>
<button type="submit" class="registerButtons" id="submitRegistration">Register</button>
</div>
</form>
The thing I want is after filling this form and submitting it to take all the textbox values and put the in my database.
The db model is implemented like this:
class User(models.Model):
username = models.CharField(max_length=200)
password = models.CharField(max_length=200)
...
def __unicode__(self):
return self.username
Here is the code of the view:
def register(request):
context = RequestContext(request)
registed = False
if request.method == 'POST':
form = User(data = request.POST)
username = request.POST.get('registerUsername')
password = request.POST.get('registerPass')
user = form.save()
username = User.username
password = User.password
u = User(username,password,...)
u.save()
registered = True
else:
form = User()
return render_to_response('ProjectName/home.html', {'user_form':User}, context)
I do not want to use the automated way of Django where it generates forms from models.
You forgot to define name in the form. It is important for getting the POST data.
<form .........>
{{error}}<br/><br/>
<div class="registerLabel">Username:</div>
<div class="registerTextLabel">
<input type="text" id="registerUsername" name="registerUsername" class="registerTextDetails">
</div>
<div class="registerLabel" id="registerLabelPass">Password:</div>
<div class="registerTextLabel">
<input type="password" id="registerPass" name="registerPass" class="registerTextDetails">
</div>
</form>
def register(request):
context = RequestContext(request)
registered = False
error = ''
if request.method == 'POST':
username = request.POST.get('registerUsername')
password = request.POST.get('registerPass')
if username and password:
username_exist = User.objects.filter(username=username)
if username_exist:
error = username is already taken, try another
else:
User.objects.create(username=username, password=password)
registered = True
else:
error = all fields are required
return render_to_response('ProjectName/home.html', {'error':error}, context)
Shouldn't this
username = User.username
password = User.password
be
User.username = username
User.password = password
This is my html file:
{% block content %}
<div class="container">
<h6 class="dress">Home <i> </i> Register </h6>
</div>
<div class="back">
<h2>PRODUCTS</h2>
</div>
<div class="container">
<div class="register">
<h3>PERSONAL INFORMATION</h3>
<form method='POST' action=''>{% csrf_token %}
<div class="mation">
<div>
<span>User Name</span>
<input id="id_username" maxlength="150" name="username" type="text" required />
<span class="helptext">Required. 150 characters or fewer. Letters, digits and #/./+/-/_ only.</span></p>
</div>
<div>
<span>Email Address</span>
<input id="id_email" maxlength="254" name="email" type="text" />
</div>
<div>
<span>Password</span>
<input id="id_password1" name="password1" type="password" required />
</div>
<div>
<span>Confirm Password</span>
<input id="id_password2" name="password2" type="password" required />
</div>
</div>
<input type="submit" value="Register">
</form>
</div>
</div>
{% endblock %}
Now You can see my RegisterFrom:
class RegistrationForm(forms.ModelForm):
password1 = forms.CharField(label="Password", widget=forms.PasswordInput())
password2 = forms.CharField(label="Confirm Password", widget=forms.PasswordInput())
class Meta:
model = User
fields = ['username', 'email']
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
try:
if ((password1 and password2) and (password1 != password2)):
raise forms.ValidationError("Password Do not Match")
except:
pass
return password2
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
Like this I am using custom textboxes in Django