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

  • SEARCH
  • Home
  • 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 8659033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:54:24+00:00 2026-06-12T15:54:24+00:00

I am new to django, and have been tying to pass a User object

  • 0

I am new to django, and have been tying to pass a User object to a ModelForm and then validate it. That is adding the User object as a ForeignKey to a Note object in the end, where the ModelForm is a Meta of the class Note.

My forms.py:

  class NoteForm(ModelForm):
     class Meta:
        model = Note

My views.py:

  def addNote(request):
     if request.method == 'POST':
        user = User.objects.get(username=request.POST['user'])
        model_form = NoteForm(request.POST, request.FILES, user)
        if model_form.is_valid():
           model_form.save()
           return HttpResponseRedirect(reverse('index'))
        return HttpResponse('De indtastede data er ikke gyldige')
     return render(request, 'studies/uploadfile.html')

My template.html:

<form enctype="multipart/form-data" method="post" action="/notes/add/">
Note Title: <input type="text" name="name"  /> <br />
Select Note: <input type="file" name="note" /> <br /> 
<input type="hidden" name="user" value="{{ user.id }}">
<input type="submit" value="submit" />
{% csrf_token %}
</form>

I have tried using the request.user, since im trying to get the current user logged on and adding that user as the ForreignKey.
Any help will be appreciated, beforehand thanks.

  • 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-12T15:54:26+00:00Added an answer on June 12, 2026 at 3:54 pm

    I’m not sure what the point is of wanting to send it to the template. You have it in the view both before and after validation, after all: better to deal with it there.

    The thing to do is to exclude the user field from the form definition, then set it manually on save:

    class NoteForm(ModelForm):
       class Meta:
          model = Note
          exclude = ('user',)
    
    
    if request.method == 'POST':
       model_form = NoteForm(request.POST, request.FILES)
       if model_form.is_valid():
           note = model_form.save(commit=True)
           note.user = request.user
           note.save()
           return...
    

    Also note that your view never sends any validation errors to the template, and your template doesn’t show errors or the invalid values that the user has entered. Please follow the structure set out in the documentation.

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

Sidebar

Related Questions

I am rather new to django templates and have an impression that I have
I'm new to Django, but the application that I have in mind might end
I'm new to Django and have only been using sqlite3 as a database engine
I am relatively new to Django and have really been struggling with an implementation
I've got a django project working on a development server(ubuntu) that we have been
I am brand new to Python, and have been trying to get the Django
I'm new to Django (and Python) and I have been trying to work out
I have been trying to use the new validators that are now included with
I have a django project that I have been using for a while, but
I'm new to django and I've been playing around with uploading pictures then displaying

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.