My site is set up so there is no username (or rather user.username = user.email). Django has an error message if a user tries to input a username that is already in the database, however since I'm not using a username for registration I can't figure out how to do this.
Just like the default settings already is, I don't want to reload the page to find out if there is an email address already associated with a user. My guess is to use Ajax, but I can't figure out how to do it. Ive looked at other posts, but there doesn't seem to be anything recent.
How can I check to see if an email address already exists, and if so, give an error message for the user to input a new email address?
models.py:
class MyUsers(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
email = models.EmailField(max_length=100, blank=True, unique=True)
company = models.CharField(max_length=100, blank=True, null=True)
website = models.URLField(max_length=100, blank=True, null=True)
phone_number = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return self.user.username
forms.py:
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('email',)
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'company', 'website', 'phone_number')
views.py:
def index(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.password = ""
user.username = user.email
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.email = user.email
profile.save()
user.first_name = profile.first_name
user.last_name = profile.last_name
user.save()
registered = True
return HttpResponseRedirect(reverse('registration'))
else:
print user_form.errors, profile_form.errors
else:
user_form = UserForm()
profile_form = UserProfileForm1()
context = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}
return render(request, 'mysite/register.html', context)
register.html:
{% extends 'mysite/base.html' %}
{% load staticfiles %}
{% block title_block %}
Register
{% endblock %}
{% block head_block %}
{% endblock %}
{% block body_block %}
<form id="user_form" method="post" action="/mysite/" enctype="multipart/form-data">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" name="submit" value="Register" />
</form>
{% endblock %}
You can override the clean_<INSERT_FIELD_HERE>() method on the UserForm to check against this particular case. It'd look something like this:
forms.py:
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('email',)
def clean_email(self):
# Get the email
email = self.cleaned_data.get('email')
# Check to see if any users already exist with this email as a username.
try:
match = User.objects.get(email=email)
except User.DoesNotExist:
# Unable to find a user, this is fine
return email
# A user was found with this as a username, raise an error.
raise forms.ValidationError('This email address is already in use.')
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'company', 'website', 'phone_number')
You can read more about cleaning specific fields in a form in the Django documentation about forms.
That said, I think you should look into creating a custom user model instead of treating your User Profile class as a wrapper for User.
Related
I'm trying to create a custom form where the user can also enter his name for instance but I am facing an issue, when the registration is done the name is not saved and I can't show it on the template.
here is the code
views.py
def register_user(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/user/')
else:
if request.method == 'POST':
form = MyRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/user/')
context = {}
context.update(csrf(request))
context['form'] = MyRegistrationForm()
return render(request, 'register.html', context)
forms.py
class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
name = forms.CharField(required=True)
class Meta:
model = User
fields = {'name', 'username', 'password1', 'password2', 'email'}
def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.name = self.cleaned_data['name']
if commit:
user.save()
return user
register.html
<form action="/user/register/" method="post" id="register" autocomplete="off">
{% csrf_token %}
<div class="fieldWrapper">
{{ form.name.errors }}
{{ form.name.label_tag }}
{{ form.name }}
</div>
[... other form fields ...]
</div>
<input type="submit" value="Register"/>
</form>
So when I submit the form, everything works but when I try to show {{ user.name }} in the template, nothing is shown, why is that? (it works for the email field)
The default User object doesn't have a name field (so you are actually just saving the content of your name field to the object, and not the database). It has a first_name and last_name so you can either use those fields instead or you can customize the User model to have a separate name field
Edit
Also, just so you know, if you use the first_name and last_name fields instead, the User model has a get_full_name() method built-in which might be useful
The User does not have a "name" field. Try:
{{ user.username }}
I'm trying to write a small Django system. After logging into the system, a user can edit and save his/her own profile information. The fields involved are: username, email, first name, last name, website and picture.
The problem: The picture cannot be updated (After selecting an image and clicking "update" button, it shows "No file selected". The profile picture displayed on the page is still the old one). But the other fields are all OK.
Here are my codes:
models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True)
picture = models.ImageField(upload_to="profile_images", blank=True)
def __str__(self):
return self.user.username
forms.py:
class UserForm2(forms.ModelForm):
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name')
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('website', 'picture')
views.py:
#login_required
def update_user(request):
try:
user_profile = UserProfile.objects.get(user=request.user)
except UserProfile.DoesNotExist:
return HttpResponse("invalid user_profile!")
if request.method == "POST":
update_user_form = UserForm2(data=request.POST, instance=request.user)
update_profile_form = UserProfileForm(data=request.POST, instance=user_profile)
if update_user_form.is_valid() and update_profile_form.is_valid():
user = update_user_form.save()
profile = update_profile_form.save(commit=False)
profile.user = user
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
profile.save()
else:
print(update_user_form.errors, update_profile_form.errors)
else:
update_user_form = UserForm2(instance=request.user)
update_profile_form = UserProfileForm(instance=user_profile)
return render(request,
'userprofile/update_user.html',
{'update_user_form': update_user_form, 'update_profile_form': update_profile_form}
)
update_user.html:
<form id="update_user_form" method="POST" action="/userprofile/update_user/">
{% csrf_token %}
{{ update_user_form.as_p }}
{{ update_profile_form.as_p }}
<img src="{{ update_profile_form.instance.picture.url }}" />
<br />
<input type="SUBMIT" name="submit" value="Update"/>
</form>
How can I make it work properly?
To upload the file you should add the enctype attribute to the <form> tag:
<form id="update_user_form" method="POST" action="/userprofile/update_user/"
enctype="multipart/form-data">
i want this functionality. User enters email address, and somehow it has to be passed to my views.py file, so i could then email the user that he has succesfully registered.
This is my template file:
{% extends "base.html" %}
{% block content %}
<section>
<h2 style="text-align: center">Register</h2>
<form action="/accounts/register/" method="post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" value="Register" onclick="validateForm()"/>
</form>
</section>
{% endblock %}
this is my forms.py file:
class MyRegistrationForm(UserCreationForm):
#kokie fields bus displayed html form
email = forms.EmailField(required=True)
firstname = forms.CharField(required=True)
lastname = forms.CharField(required=True)
whoinvitedyou = forms.CharField(required=True)
phone = forms.CharField(required=True)
workplace = forms.CharField(required=True)
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2', 'firstname', 'lastname', 'whoinvitedyou', 'phone', 'workplace')
def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.set_password(self.cleaned_data["password1"])
#more fields for name last name
user.firstname = self.cleaned_data['firstname']
user.lastname = self.cleaned_data['lastname']
user.whoinvitedyou = self.cleaned_data['whoinvitedyou']
user.phone = self.cleaned_data['phone']
user.workplace = self.cleaned_data['workplace']
if commit:
user.save()
return user
this is my views.py:
def register_user(request):
if request.method == 'POST':
form = MyRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
else:
return render_to_response('invalid_reg.html')
args = {}
args.update(csrf(request))
args['form'] = MyRegistrationForm()
print args
return render_to_response('register.html', args)
how do i pass the value so later i can use it? maybe somebody can help me with this...
In your view, after the form.is_valid() call, the email address will be available in form.cleaned_data['email']. You can use that to send the email after form.save().
Additionally, you might want to look into existing 3rd party libraries like django-registration as it already does the functionality (emailing the just registered user) that you want.
In order to send an e-mail you don't necessarily need to send the value to a view in views.py.
You can use a post_save signal to send an email. You can put the code anywhere, although I usually put it in models.py.
Info on signals: https://docs.djangoproject.com/en/1.7/topics/signals/
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail
#receiver(post_save, sender=User)
def my_handler(sender, instance, *args, **kwargs):
send_mail('Subject of email', 'Message body.', 'from#example.com', [instance.email], fail_silently=False)
note that instance.email is the email address of the user which you just saved, you can access instance to retreive more information e.g. the name, so that you can put "dear "+instance.name at the beginning of the body for example
I've been tried many different things to solve this issue and none seem to work.
Essentially I am just trying to create a registiration page which contains 2 forms built from 2 models, the User model and my restaurant model (profile model). Every time I submit my form, I keep getting AttributeError 'tuple' object has no attribute 'get'. I thought it may have been my cuisine many to many field causing the issue as its the only thing that is a tuple I think. So I tried removing the cuisine aspect altogether but that didn't work. Heres my code. I can provide additional information if necessary. Appreciate any help very much
Traceback - dpaste.com/2CXS7MZ
Additional Information
Request Method: POST
Request URL: http://127.0.0.1:8000/auth/register/
Django Version: 1.6.5
Exception Type: AttributeError
Exception Value:
'tuple' object has no attribute 'get'
Exception Location: C:\Python34\testapps\lib\site-packages\django\middleware\clickjacking.py in process_response, line 30
Python Executable: C:\Python34\testapps\Scripts\python.EXE
Python Version: 3.4.1
Models
class Cuisine(models.Model):
name = models.CharField(max_length=15, blank=False)
def __str__(self):
return self.name
#User Profile (The restaurant)
class Restaurant(models.Model):
#Main Test Field
name = models.CharField(max_length=25, blank=False)
user = models.OneToOneField(User)
cuisine = models.ManyToManyField(Cuisine, blank=True)
approved = models.BooleanField(default=False)
#Non Essential Fields
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
Forms
class UserSignUpForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'password', 'first_name', 'last_name', 'email',)
class RestaurantForm(forms.ModelForm):
cuisine = forms.ChoiceField(queryset=Cuisine.name.all())
name = forms.CharField(max_length=25)
class Meta:
model = Restaurant
exclude = ('user', 'approved',)
Views
def restaurant_register(request):
if request.user.is_authenticated():
return HttpResponse('Thank you for registering.')
if request.method == 'POST':
user_form = UserSignUpForm(request.POST)
restaurant_form = RestaurantForm(request.POST)
if user_form.is_valid() and restaurant_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = restaurant_form.save(commit=False)
profile.user = user
profile.save()
profile.save_m2m()
return render(request, '/auth/login.html')
else:
return user_form.errors, restaurant_form.errors
else:
user_form = UserSignUpForm()
restaurant_form = RestaurantForm()
return render(request, 'auth/register.html', {'restaurant_form':restaurant_form, 'user_form': user_form})
Template
{% extends "base.html" %}
{% block content %}
<form id="user_form" method="post" action="/auth/register/" enctype="multipart/form-data">
{{ user_form.errors }}
{{ restaurant_form.errors }}
{% csrf_token %}
{{ user_form.as_p }}
{{ restaurant_form.as_p }}
<input type="submit" name="submit" value="Register" />
</form>
{% endblock %}
URL
url(r'^register/$', views.restaurant_register, name='register'),
You are not returning a HttpResponse here:
return user_form.errors, restaurant_form.errors
return this instead:
render(request, 'auth/register.html', {'restaurant_form':restaurant_form, 'user_form': user_form})
or rather, you should skip that whole else and return.
Here is your problem:
return user_form.errors, restaurant_form.errors
You returning tuple instead of a response
I am trying to use django's form module to create and display a form. The following code I have executes without errors but an HTML form is not populated.
I have made sure that I am loading correct template. I also am certain that correct view is triggered when I go to a certain URL. I am posting some relevant code below from views, model, forms, and template files. I did debugging for quite a bit by trial-and-error but could not get to the root of issue.
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from app.forms import RegistrationForm
def registration(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/profile/')
if request.method == 'POST':
pass
else:
''' user is not submitting the form, show them a blank registration form '''
form = RegistrationForm()
context = {'form': form}
return render_to_response('register.html', context, context_instance=RequestContext(request))
register.html
{% extends "base.html" %}
{% block content %}
<form action="/register/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
form.py
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from app.models import Agent
class RegistrationForm(ModelForm):
username = forms.CharField( label=(u'User Name'), required=True )
first_name = forms.CharField( label=(u'First Name'), required=True )
last_name = forms.CharField( label=(u'Last Name'), required=True )
birthday = forms.DateField( label=(u'Date of birth'), required=True )
email = forms.EmailField( label=(u'Email Address'), required=True )
password = forms.CharField( label=(u'Password'), widget=forms.PasswordInput(render_value=False), required=True )
password_confirm = forms.CharField( label=(u'Confirm Password'), widget=forms.PasswordInput(render_value=False), required=True )
class Meta:
model = Agent
#exclude = ['username',]
def clean_username(self):
username = self.cleaned_data['username']
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError("That username is already taken, please select another.")
def clean(self):
if self.cleaned_data['password'] != self.cleaned_data['password_confirm']:
raise forms.ValidationError("The passwords did not match. Please try again.")
return self.cleaned_data
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class Agent(models.Model):
username = models.OneToOneField(User)
birthday = models.DateField()
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField()
def __unicode__(self):
return self.last_name + ", " + self.first_name
Had to add content block ref in base.html
{% block content %}
{% endblock %}