简体   繁体   中英

How can I give an error when someone edits there profile and changes the email to an email that already exists using django?

I want to give an error that an account with a email already exists, but it doesn't seem to work when a user edits there profile. Also, if the email isn't valid when submitting the form it it gives a value error: "The view accounts.views.edit_profile didn't return an HttpResponse object. It returned None instead."

Here is the code:

{% extends 'base.html' %}
{% block head %}
    <link href="\static\accounts\css\forms.css" rel="stylesheet">
{% endblock %}
{% block body %}
  <h3 style="text-align: center">Edit profile</h3>
  <form id="login-form" method="post">

                {% csrf_token %}
            <input placeholder="Email" id="id_email" name="email"
           type="text" class="form-control" value="{{ user.email}}">
            <input placeholder="First name" id="id_first_name" name="first_name"
                       type="text" class="form-control" value="{{ user.first_name}} ">
            <input placeholder="Last name" id="id_last_name" name="last_name"
                       type="text" class="form-control" value="{{ user.last_name}}">
                {% if form.errors %}
                    {% for field in form %}
                        {% for error in field.errors %}
                            <p class=" label label-danger">
                        <div style="text-align: center">
                            {{ error }}
                        </div>
                    </p>
                        {% endfor %}
                    {% endfor %}
                    {% for error in form.non_field_errors %}
                        <div class="alert alert-danger">
                            <strong>{{ error }}</strong>
                        </div>
                    {% endfor %}
                {% endif %}
                <div style="text-align:center;">
                    <button type="submit" class="btn btn-outline-dark centerbutton">Save edits</button>
                </div>

            </form>
{% endblock %}

Also, here is the code I used to check if the email exists in the registration form, but it doesn't seem to work for the edit profile form:

def clean_email(self):
    email = self.cleaned_data.get('email')
    username = self.cleaned_data.get('username')
    if email and User.objects.filter(email=email).exclude(username=username).exists():
        raise forms.ValidationError(u'An account with this email address already exists')
    return email

And my editprofileform:

class EditProfileForm(UserChangeForm):

    class Meta:
        model = User
        fields = (
            'email',
            'first_name',
            'last_name',
            'password'
        )

This is too large of a question to answer on here and I won't be posting any code for you. You will need to read some docs and work on it yourself.

Checking that email already exists... you will likely have to go to your database or for that. Enforcing that an email column be unique in your database is a good way to handle this. You can catch the database errors and handle them as you like.

As for the valid email validation, you will want to use Django's validators. One of the validators is specifically for checking valid email format.

https://docs.djangoproject.com/en/2.2/ref/validators/#emailvalidator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM