Is it possible to get request.user data in a form class? I want to clean an email address to make sure that it's unique, but if it's the current users email address then it should pass.
This is what I currently have which works great for creating new users, but if I want to edit a user I run into the problem of their email not validating, because it comes up as being taken already. If I could check that it's their email using request.user.email then I would be able to solve my problem, but I'm not sure how to do that.
class editUserForm(forms.Form): email_address = forms.EmailField(widget=forms.TextInput(attrs={'class':'required'})) def clean_email_address(self): this_email = self.cleaned_data['email_address'] test = UserProfiles.objects.filter(email = this_email) if len(test)>0: raise ValidationError("A user with that email already exists.") else: return this_email
Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.
Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.
GET and POSTDjango's login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response. GET , by contrast, bundles the submitted data into a string, and uses this to compose a URL.
As ars and Diarmuid have pointed out, you can pass request.user
into your form, and use it in validating the email. Diarmuid's code, however, is wrong. The code should actually read:
from django import forms class UserForm(forms.Form): email_address = forms.EmailField( widget=forms.TextInput( attrs={ 'class': 'required' } ) ) def __init__(self, *args, **kwargs): self.user = kwargs.pop('user', None) super(UserForm, self).__init__(*args, **kwargs) def clean_email_address(self): email = self.cleaned_data.get('email_address') if self.user and self.user.email == email: return email if UserProfile.objects.filter(email=email).count(): raise forms.ValidationError( u'That email address already exists.' ) return email
Then, in your view, you can use it like so:
def someview(request): if request.method == 'POST': form = UserForm(request.POST, user=request.user) if form.is_valid(): # Do something with the data pass else: form = UserForm(user=request.user) # Rest of your view follows
Note that you should pass request.POST as a keyword argument, since your constructor expects 'user' as the first positional argument.
Doing it this way, you need to pass user
as a keyword argument. You can either pass request.POST
as a positional argument, or a keyword argument (via data=request.POST
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With