Responsive Ad Area

Share This Post

test

IntegrityError: null value in column "image" violates not-null constraint

When I register a new user the website was creating a profile automatically and was using default.jpg image. Suddenly it stopped working. And the error says that I have a problem with my ‘image’ field. This is the error I get:

IntegrityError at /accounts/register/

null value in column "image" violates not-null constraint
DETAIL:  Failing row contains (20, pbkdf2_sha256$150000$72dkND5yT5M0$+/chH/5Vu76KM7oNMjU694EWFZ/p+B..., null, f, kazanda, f, 2019-04-21 17:41:29.969889+00, Andreas, Swarchft, Germany, Frankfurt, andreas_sw@gmail.com, t, null).

This is my models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics', null=True)

    def __str__(self):
        return self.user.username

    def save(self, force_insert=False, force_update=False, using=None):
        super().save()

        img = Image.open(self.image.path)
        if img.height > 250 or img.width > 250:
            output_size = (250, 250)
            img.thumbnail(output_size)
            img.save(self.image.path)



def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = Profile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)

my views.py

class UserRegistrationView(SuccessMessageMixin, CreateView):
    form_class = UserCreationModelForm
    model = User
    success_url = reverse_lazy('login')
    success_message = "Account for %(first_name)s was created successfully. You will get email notification when admin will activate your account!"
    template_name = 'users/registration.html'

    def get_success_message(self, cleaned_data):
        return self.success_message % dict(cleaned_data)


@login_required
def profile(request):
    if request.method == 'POST':
        uform = UserUpdateForm(request.POST, instance=request.user)
        pform = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        posts = Post.objects.filter(author=request.user)

        if uform.is_valid() and pform.is_valid():
            uform.save()
            pform.save()
            messages.success(request, 'Your account has been updated!')
            return redirect('users:cabinet')
    else:
        uform = UserUpdateForm(instance=request.user)
        pform = ProfileUpdateForm(instance=request.user.profile)
        posts = Post.objects.filter(author=request.user)

    context = {
        'uform': uform,
        'pform': pform,
        'posts': posts
    }

    return render(request, 'users/user_detail.html', context)



@login_required
def cabinet(request):
    profile = Profile.objects.all()

    context = {
        'profile': profile

    }
    return render(request, 'users/user_detail.html', context)

my registration.html

which is a form to create a user:

{% extends 'shared/base.html' %}
{% load staticfiles %}
{% load crispy_forms_tags %}

{% block content %}
<div class="content-section mt-5 pl-5 p-4">
  <form method="post">
    {% csrf_token %}
    <fieldset class="form-group pr-4">
      <legend class="mb-4">Join Today</legend>
      <div class="form-row">
        <div class="form-group col-md-3 mb-0">
          {{ form.first_name|as_crispy_field }}
        </div>
        <div class="form-group col-md-3 mb-0">
          {{ form.last_name|as_crispy_field }}
        </div>
        </div>
        <div class="form-row">
        <div class="form-group col-md-6 mb-0">
          {{ form.username|as_crispy_field }}
        <div class="form-row">
        <div class="form-group col-md-6 mb-0">
          {{ form.country|as_crispy_field }}
        </div>
        <div class="form-group col-md-6 mb-0">
          {{ form.city|as_crispy_field }}
        </div>
        <div class="form-row">
        <div class="form-group col-md-12 mb-0">
          {{ form.email|as_crispy_field }}
        </div>
        <div class="form-row">
        <div class="form-group col-md-6 mb-0">
          {{ form.password1|as_crispy_field }}
        </div>
        <div class="form-group col-md-6 mb-0">
          {{ form.password2|as_crispy_field }}
        </div>
        <!-- <div class="form-check">
              {{ form.access_challenge }}
          <label class="form-check-label" for="Checkbox">&nbsp;Access Challenge Country</label>
        </div> -->
    </fieldset>
    <div class="form-group">
      <button style="border-radius: 0; width: 200px; padding-left: 4px;" class="btn btn-info btn-block" type="submit">Sign Up</button>
    </div>
  </form>
  <div class="border-top pt-3">
    <small class="text-muted">
      Already Have An Account? <a class="ml-2" href="{% url 'login' %}">Sign In</a>
    </small>
  </div>
</div>
{% endblock %}


IntegrityError: null value in column “image” violates not-null constraint
IntegrityError: null value in column “image” violates not-null constraint
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar