Django send email from a CBV without having a model - django

I need to send with Django email when a user completes a contact form.
Because the information, from the form is not kept in Django database, I don't have models for it, just get the information and send email.
I'm using Class Based Views.
I'm thinking of inheriting from View, but where(method) and how I hook my sending email ?
Can I use just inputs(direct html form), or I need to create a Django form ?

You should create a Django form. Then your view can inherit from FormView, and your mail sending functionality would go in form_valid.

Related

Django: all auth create account with email - unique constraint failed. Display message instead of giving an error

I am using djnago all-auth to create custom user accounts. When creating an account with email and password, if account with a email already exits it gives an error (UNIQUE constraint failed: account_emailaddress.email) but I would like to display message that an account with this email already exists instead of throwing an error. What is the best way to handle this? In general I would use AJAX to verify and display message for my own views but I do not know how to deal here with django all-auth package.
I'll suggest that you should override the signup/login form in order to manage this error. Have you checked the documentation? https://django-allauth.readthedocs.io/en/latest/forms.html
I think this answer is related to your question.
A relatively similar approach is given in this answer:
Create your custom view that inherits SignupView and overrides the form class
Create a custom form that inherits from SignupForm and overrides the email validation message
In your own urls.py add the following after include('allauth.urls') to override the account_signup url
Since djangoallauth take care of unique constrain you don't have to add unique=True to your field if user try to login with any social media account with email id already present in your database it djangoallauth will simple ignore and will not set email id in your user model. :)
I am handling my unique fields i.e Email field manually

Does Django have method to 'validate' form fields BEFORE form submission?

I have read Django documentation and found almost 2 ways to validate fields forms (clean_data, validators) but I am looking for a method to test fields before form submission
i can do it with JS but I wonder if there is a way to do it with Django
It can't be done before submission - how the server would now what user put into each field? It can only validate the data when it gets it - after user submits a form.

Django custom user model and registration

I have a Django application where I enabled login and registration with Tivix's django-rest-auth (https://github.com/Tivix/django-rest-auth).
Now,
I want to have a custom user model with few additional fields
I want to be able to enable registration along with these additional fields.
Login would be done with email instead of username
What are the steps to achieve this? I went through answers, however I was confused with few things:
The role of serializers here and custom user signUp form and whether I should have a signUp view as well?
Custom user model from AbstractBaseUser or simply Userof django.contrib.auth.models
I already tried to implement something and ran into an issue (django-rest-auth custom registration fails to save extra fields), so wanted to ask for a correct approach.

How to apply DRY to Django Models, forms

I'm working on a website and simultaneously on a mobile app.
For the website I've created a Django Form for the User model which override the clean and valid_ methods to provide custom validation.
For the mobile app I'm doing a REST API which exposes and endpoint to create a new user using Tastypie for this.
My doubt is where and how should I define the custom validation for the User model applying the DRY principle.
I can create a UserForm from the data provided by the user in the mobile app, then use the method is_valid() for validating the data, but the form contains a password_confirm field which is also validated and in the app this field doesn't exist.
Should I create a UserForm specific for the mobile app? Should I move these validations to the model class?
The simplest way is probably to subclass the form. Create a LoginForm that does not have the password2 field, and use that one for the mobile login API. Then make a subclass called WebLoginForm and add a password2 field, as well as a clean_password2() method that includes the validation logic. That way, you won't even need to override any of your other clean() code.
An even easier--though hackish--solution would be to have the mobile app submit the password the user wrote both as "password1" and "password2" when it makes the API call.

how to customizing fields in django's comment app?

How can I customize the django builtin comment app so that the fields url, email will be ignored and automatically populate the name with the user's username since I'm only allowing comments from authenticated users?
Start here: http://docs.djangoproject.com/en/1.1/ref/contrib/comments/custom/#ref-contrib-comments-custom
Extend the save method to pull user data and set it to comment's field