Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:06:32+00:00 2026-06-15T12:06:32+00:00

I’m trying to make it so a user can upload an avatar to their

  • 0

I’m trying to make it so a user can upload an avatar to their UserProfile, but I keep getting the following error:

IntegrityError at /~csihar/userpic
null value in column "user_id" violates not-null constraint
DETAIL:  Failing row contains (4, null, userpics/image.jpg).

…with the first value incrementing by 1 each time I try to upload. I’ve attempted to instantiate the form with the user’s current profile before saving, as well as excluding irrelevant fields from the modelform, but it still isn’t working and I’m not sure what I’m overlooking. However, the image does get saved to my MEDIA_ROOT before the error occurs.

models.py:

from django.contrib.auth.models import User
from django.forms import ModelForm
from django_resized import ResizedImageField

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    userpic = ResizedImageField(max_width=100, max_height=100, upload_to='userpics/')

class UserpicUpload(ModelForm):
    class Meta:
        model = UserProfile
        exclude = ('user','user_id')

views.py:

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from items.models import User, UserProfile, UserpicUpload
from django.contrib.auth.decorators import login_required

@login_required
def userpic(request,list_owner):
    current_user = User.objects.get(pk=request.user.id)
    current_profile = current_user.get_profile()
    if request.method == 'POST':
        form = UserpicUpload(request.POST, request.FILES, instance=current_profile)
        if form.is_valid():
            newuserpic=UserProfile(userpic=request.FILES['userpic'])
            newuserpic.save()
        gohomeurl = "/~" + request.user.username
        return HttpResponseRedirect(gohomeurl)
    else:
        form = UserpicUpload()
        return render_to_response('userpic.html', {'listowner': list_owner, 'form':form}, RequestContext(request))

forms.py:

from django import forms

class UserpicUpload(forms.ModelForm):
    userpic  = forms.ImageField()

template (userpic.html):

{% extends 'base.html' %}
{% block body %}
<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
<p>{{ form.non_field_errors }}</p>
<p>{{ form.userpic.errors }}</p>
<p>{{ form.userpic }}</p>
<p><input name="submit" value="Upload" type="submit" />
</form>
{% endblock %}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:06:33+00:00Added an answer on June 15, 2026 at 12:06 pm

    Turns out I was going about it all wrong and was creating new profiles rather than adding to an existing one. The problem was fixed by replacing the following lines in views.py:

    newuserpic=UserProfile(userpic=request.FILES['userpic'])
    newuserpic.save()
    

    …with this:

    newuserpic=request.FILES['userpic']
    current_profile.userpic=newuserpic
    current_profile.save()
    

    Now I’m having issues getting the pic to display properly on the user’s home page, but that’s a whole separate issue that probably has to do with my MEDIA_URL – at least the pic is getting saved properly now!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.