Hi I have a simple django form, which enables the users to signup to the website. but am confused how can I submit my form fields. Am new to Django. Please help me on it. Thanks in advance.
Forms.py:
from django import forms
from django import forms
from django.contrib.auth.models import User # fill in custom user info then save it
# from django.contrib.auth.forms import UserCreationForm
class UserForm(forms.Form):
email = forms.EmailField(max_length=100, null=True, blank=False)
first_name = forms.CharField(max_length=20)
password = forms.CharField(max_length=20, required=True, label="password", widget=forms.PasswordInput)
last_name = forms.CharField(max_length=20)
date_joined = forms.DateTimeField(auto_now_add=True, auto_now=False)
date_ = forms.DateTimeField(auto_now_add=False, auto_now=True)
Views.py
def register_user(request):
if request.method == 'POST':
print "Saisisis"
form = UserForm(request.POST) # create form object
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
print "blah"
args = {}
args.update(csrf(request))
args['form'] = UserForm()
# import pdb
# pdb.set_trace()
print args
return render(request, 'pages/signup.html', args)
and my html:
{% extends 'pages/base.html' %}
{% block additional_styles %}
<style>
body{
background:url(static/img/nose.jpg) no-repeat center center fixed;
-webkit-background-size: cover
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>
{% endblock %}
{% block contentblock %}
<div class="container well">
<h1> Please Sign Up fellas</h1>
<form method="POST" action="login.html">
{% csrf_token %}
{{ form.as_table }}
<input type="submit" value="OK">
</form>
</div>
{% endblock %}
To do what you've got there, you'd need to have a ModelForm so that when you call form.save() Django knows what the model you are creating an instance of. For example;
Forms.py:
from django import forms
from django.contrib.auth.models import User
class UserForm(forms.ModelForm):
email = forms.EmailField(max_length=100, null=True, blank=False)
first_name = forms.CharField(max_length=20)
password = forms.CharField(max_length=20, required=True, label="password", widget=forms.PasswordInput)
last_name = forms.CharField(max_length=20)
date_joined = forms.DateTimeField(auto_now_add=True, auto_now=False)
date_ = forms.DateTimeField(auto_now_add=False, auto_now=True)
class Meta:
model = User
But going from what you've got you'd need to create the model instance yourself, then set the data, then save it;
def register_user(request):
if request.method == 'POST':
form = UserForm(request.POST) # create form object
if form.is_valid():
email = form.cleaned_data['email']
user = User(email=email)
user.save()
return HttpResponseRedirect('/accounts/register_success')
Related
I am working on a forum using django, I have a problem accessing user fullname and bio, from a model class I have. I have no problem accessing the user.username or user.email, but not from the Author class..
This is from the models.py in the forum app
User = get_user_model()
class Author(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
fullname = models.CharField(max_length=40, blank=True)
slug = slug = models.SlugField(max_length=400, unique=True, blank=True)
bio = HTMLField()
points = models.IntegerField(default=0)
profile_pic = ResizedImageField(size=[50, 80], quality=100, upload_to="authors", default=None, null=True, blank=True)
def __str__(self):
return self.fullname
My form is in the user app, where i have a profile update site, and the form is like this
from forums.models import Author
class UpdateForm(forms.ModelForm):
class Meta:
model = Author
fields = ('fullname', 'bio', 'profile_pic')
Then here is some of the update site, however nothing let me get access to the bio or fullname, I've tried so many combos. and I am lost here..
{% block content %}
<section class="section" id="about">
<!-- Title -->
<div class="section-heading">
<h3 class="title is-2">Hey {{ user.username }}</h3>
<div class="container">
<p>{{ user.bio }}bio comes here</p>
</div>
</div>
Here is the view.py from the user app
from .forms import UpdateForm
def update_profile(request):
context = {}
user = request.user
instance = Author.objects.filter(user=user).first()
if request.method == 'POST':
form = UpdateForm(request.POST, request.FILES, instance=user)
if form.is_valid():
form.instance.user = user
form.save()
return redirect('/')
else:
form = UpdateForm(instance=user)
context.update({
'form': form,
'title': 'update_profile',
})
return render(request, 'register/update.html', context)
The form html
<form method="POST" action="." enctype="multipart/form-data">
{% csrf_token %}
{{form|crispy}}
<hr>
<button class="button is-block is-info is-large is-fullwidth">Update <i class="fa fa-sign-in" aria-hidden="true"></i></button>
</form>
If there is some relation i am missing please help
Your view currently creates a new Author record each time you "update" the model. I would advise to first clean up the database and remove all authors.
Then you can convert the ForeignKey into a OneToOneField here: that way we know that each user has at most one Author:
from django.conf import settings
class Author(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
# …
Now we can alter the view to create a record in case there is no such record, or update an existing record if there is one:
from .forms import UpdateForm
def update_profile(request):
context = {}
user = request.user
instance = Author.objects.filter(user=user).first()
if request.method == 'POST':
form = UpdateForm(request.POST, request.FILES, instance=instance)
if form.is_valid():
form.instance.user = user
form.save()
return redirect('/')
else:
form = UpdateForm(instance=instance)
context.update({
'form': form,
'title': 'update_profile',
})
return render(request, 'register/update.html', context)
In the template, you can render data of the related Author model for a user user with:
{{ user.author.fullname }}
{{ user.author.bio }}
I would like to create a contact form on my Django website.
For now, this is my code:
models.py:
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
class Client(models.Model):
phone = PhoneNumberField(null=False, blank=True, unique=True)
forms.py:
from django import forms
from phonenumber_field.modelfields import PhoneNumberField
class ContactForm(forms.Form):
fullName = forms.CharField(max_length=100)
email = forms.EmailField()
phone = PhoneNumberField()
message = forms.CharField(widget=forms.Textarea)
views.py:
def contact(request):
# return render(request, 'contact.html')
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# send email code goes here
return HttpResponse('Thanks for contacting us!')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
html:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
I of course installed phonenumber_field and added it in settings.py
This is the result, phone field missing:
Any help is hugely appreciated! Thanks for your time.
You used a model field, whereas for a form, you need a form field:
from django import forms
# a form field ↓
from phonenumber_field.formfields import PhoneNumberField
class ContactForm(forms.Form):
fullName = forms.CharField(max_length=100)
email = forms.EmailField()
phone = PhoneNumberField()
message = forms.CharField(widget=forms.Textarea)
I have a view which renders 2 forms to a template, but only one renders, the other doesnt display and it doesnt give me any error, but I can see that the form display when I print it in my console.
This is my model for the form not showing
class Organization(models.Model):
name = models.CharField(max_length=255, null=True)
This is the model for the admin, Im inheriting from AbstractUSer
class User(AbstractUser):
is_user = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
This is the form for the model
class OrganizationForm(forms.ModelForm):
name = forms.CharField(max_length=255)
class Meta:
model = Organization
fields = ['name']
This is the form for the Admin
class AdminSignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User
fields = ['username','email']
def save(self, commit=True):
user = super().save(commit=False)
user.is_admin = True
if commit:
user.save()
return user
This is the view which I am calling the multiple forms
def signup(request):
if request.method == 'POST':
adminForm = AdminSignUpForm(request.POST)
orgForm = OrganizationForm(request.POST)
if adminForm.is_valid() and orgForm.is_valid():
adminForm.save()
orgForm.save(commit=False)
username = adminForm.cleaned_data.get('username')
raw_password = adminForm.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('myapp:home')
else:
adminForm = AdminSignUpForm()
orgForm = OrganizationForm()
print(orgForm)
return render(request, 'registration/signup_form.html', {'OrgFrom': orgForm,'Adminform': adminForm})
And this is the template I am rendering the multiple forms
<form enctype="multipart/form-data" method="post" >
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
{{Adminform.as_p }}
{{ OrgForm.as_p }}
<button type="submit" class="btn btn-success">Sign up</button>
</form>
I expect both forms to be displayed but only the Adminform displays and it gives me no error to work with
There is one typo. You need to use {{ OrgFrom.as_p }} instead of {{ Orgform.as_p }}.
I am currently creating an application that allows users to view and edit their own personal profile. I've recently added in the ability for a user to add a profile picture to their profile. I can add a profile in the admin page and it will show up on the selected users profile no problem. The problem is when the user tries to update their picture I am getting an ValueError telling me that the image attribute has no file associated. Below is how I have tried to implement the functionality.
Models
class UserProfileModel(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
age = models.PositiveIntegerField(blank=True, null=True)
email = models.EmailField(max_length=254, null=True, blank=True, unique=True)
height = models.PositiveIntegerField(blank=True, null=True)
weight = models.PositiveIntegerField(blank=True, null=True)
bio = models.CharField(max_length=100, blank=True, default='')
image = models.ImageField(upload_to='profile_image', blank=True)
forms
class UpdateProfile(forms.ModelForm):
class Meta:
model = UserProfileModel
fields = ('email', 'age', 'height', 'weight', 'bio', 'image')
views
def update_profile(request):
args = {}
if request.method == 'POST':
form = UpdateProfile(request.POST, instance=request.user.userprofilemodel)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('account:profile'))
# return render(request, 'account/profile.html')
else:
form = UpdateProfile()
if request.user.is_authenticated():
form = UpdateProfile(instance=request.user.userprofilemodel)
args['form'] = form
return render(request, 'account/edit_profile.html', args)
HTML
<!DOCTYPE html>
<html>
<body>
{#<h2 style="text-align:center">User Profile Card</h2>#}
<div class="container">
<h1>{{ user }}s Profile </h1>
<div style="margin: 24px 0;">
<p>Username: {{ user }}</p>
<p>Email: {{ user.userprofilemodel.email }}</p>
<p>Age: {{ user.userprofilemodel.age }}</p>
<p>Height: {{ user.userprofilemodel.height }} CM </p>
<p>Weight: {{ user.userprofilemodel.weight }} KG </p>
<p>User Bio: {{ user.userprofilemodel.bio }} </p>
<img src="{{ user.userprofilemodel.image.url }}" width="240">
</body>
</html>
Looks like you may have forgotten to pass request.FILES to your form?
form = UpdateProfile(request.POST, request.FILES, instance=request.user.userprofilemodel)
Also, you haven't shown the template containing the form used to upload an image, but ensure that the form's enctype is set to multipart/form-data.
<form method="post" enctype="multipart/form-data">
I want to modify my UserCreationForm so that when users request to sign up to my site they get given a username of last_name+'.'+first_name.
my django forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False)
last_name = forms.CharField(max_length=30, required=False)
email = forms.EmailField(max_length=254)
class Meta:
model = User
fields = ('first_name', 'last_name', 'email', 'password1', 'password2', )
exclude = ['username', ]
So I have excluded the username from the actual form:
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
and in my views.py:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
I have tried putting in form.save() here and then trying to get cleaned_data first_name.last_name = username, but it does not work
if form.is_valid():
form.save()
first_name = form.cleaned_data.get('first_name')
last_name = form.cleaned_data.get('last_name')
raw_password = form.cleaned_data.get('password1')
#username = form.cleaned_data.get('username')
username = firstname+'.'+lastname
user = authenticate(username=username, password=raw_password)
user.is_active = False
user.save()
return render(request, 'registration/signedup.html', {'user': user})
else:
return render(request, 'registration/signup.html', {'form': form, 'invalid': 'Please try again.'})
else:
form = SignUpForm()
return render(request, 'registration/signup.html', {'form': form})
By default the UserCreationForm on init checks if the User model USERNAME_FIELD attribute exists in the forms fields so you don't want to exclude the username attribute unless you've selected another field to be your USERNAME_FIELD.
If you want to use username as the USERNAME_FIELD but you don't want it shown on the front end, you can set it as hidden in your html using the template syntax like {{ form.username.as_hidden }}
You can then override the save method for the SignUpForm, this will allow you to do any post processing once the form is valid.
You also don't need to add password1 and password2 to the fields because they're inherited.
You should set the first_name and last_name to be required if you plan on setting the username attribute to be the combined value of the 2 otherwise the code i've provided below won't work when users DON'T enter their first_name and last_name.
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False)
last_name = forms.CharField(max_length=30, required=False)
email = forms.EmailField(max_length=254)
class Meta:
model = User
fields = ('email', 'username', 'first_name', 'last_name')
def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.username = '{}.{}'.format(
self.cleaned_data['last_name'],
self.cleaned_data['first_name']
)
if commit:
user.save()
return user