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 7550809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:18:42+00:00 2026-05-30T10:18:42+00:00

I’m currently working on my first Django app, which allows registered users to submit

  • 0

I’m currently working on my first Django app, which allows registered users to submit content through a basic form.

It works thus far with one caveat: when the form is displayed, the user (“Author”) is presented with a drop-down list of all users instead of automatically populating that field with the user’s name. This is obviously not acceptable.

This goal is to have the registered user’s name automatically populate the form. I’ve seen some various potential solutions to similar problems, but nothing that addresses anything this specific.

I attempted setting the Author field in the model to “unique=True,” but that resulted in a database error when migrating it.

Any insight would be greatly appreciated:

Model:

class Story(models.Model):
    title = models.CharField(max_length=100)
    topic = models.CharField(max_length=50)
    copy = models.TextField()
    author = models.ForeignKey(User)
    zip_code = models.CharField(max_length=10)
    latitude = models.FloatField(blank=False, null=False)
    longitude = models.FloatField(blank=False, null=False)
    date = models.DateTimeField(auto_now=True, auto_now_add=True)   
    def __unicode__(self):
         return " %s" % (self.title)

Form:

class StoryForm(forms.ModelForm):
class Meta:
    model = Story

View:

@login_required
 def submit_story(request):
if request.method == "GET":
    story_form = StoryForm()
    return render_to_response("report/report.html",
                             {'form': story_form},
                              context_instance=RequestContext(request))
elif request.method =="POST":
    story_form = StoryForm(request.POST) 
    if story_form.is_valid():
        new_story = Story()
        new_story.title = story_form.cleaned_data["title"]
        new_story.topic = story_form.cleaned_data["topic"]
        new_story.copy = story_form.cleaned_data["copy"]
        new_story.author = request.user
        new_story.zip_code = story_form.cleaned_data["zip_code"]
        new_story.latitude = story_form.cleaned_data["latitude"]
        new_story.longitude = story_form.cleaned_data["longitude"]
        new_story.save()
        return HttpResponseRedirect("/report/all/")
    else:
        story_form = StoryForm()
        return render_to_response("report/report.html",
                                {'form': story_form},
                                 context_instance=RequestContext(Request))

EDIT: I think I found the relatively simple answer: I just removed the ‘author” field from the form and kept the view the same. I was able to post under the registered user’s name this way. I think this works, unless something I’m unaware of (which is plenty) is incorrect or bad protocol.

  • 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-05-30T10:18:44+00:00Added an answer on May 30, 2026 at 10:18 am

    I managed to do something similar:

    Base class for all models who are associated with a user:

    class UserOwnedModel(models.Model):
        user = models.ForeignKey(User, editable=True)
    
        class Meta:
            abstract = True
    

    Base class for all forms who are associated with a user:

    class UserOwnedForm(forms.ModelForm):
        exclude = ["user", ]
    
        def __init__(self, user, data=None, *arguments, **keywords):
            if data:
                data['user'] = user.id
                forms.ModelForm.__init__(self, data=data, *arguments, **keywords)
    

    I’m not sure if it’s the best solution (and will be glad for any input or suggestions) but it works for me.
    This of course completely removes the user field from the form, so if you need to display the user name, you’ll have to play with the code.


    EDIT

    Also, instead of this:

    new_story = Story()
    new_story.title = story_form.cleaned_data["title"]
    new_story.topic = story_form.cleaned_data["topic"]
    new_story.copy = story_form.cleaned_data["copy"]
    new_story.author = request.user
    new_story.zip_code = story_form.cleaned_data["zip_code"]
    new_story.latitude = story_form.cleaned_data["latitude"]
    new_story.longitude = story_form.cleaned_data["longitude"]
    new_story.save()
    

    You can do this:

    new_story = story_form.save()
    

    EDIT 2

    Something like this:

    class Story(UserOwnedModel):
        title = models.CharField(max_length=100)
        topic = models.CharField(max_length=50)
        copy = models.TextField()
        zip_code = models.CharField(max_length=10)
        latitude = models.FloatField(blank=False, null=False)
        longitude = models.FloatField(blank=False, null=False)
        date = models.DateTimeField(auto_now=True, auto_now_add=True)   
        def __unicode__(self):
             return " %s" % (self.title)
    
    class StoryForm(UserOwnedForm):
        class Meta:
            model = Story
    
    @login_required
    def submit_story(request):
        if request.method == "GET":
            story_form = StoryForm(user=request.user)
        ....
        elif request.method =="POST":
            story_form = StoryForm(data=request.POST, user=request.user)
            if story_form.is_valid():
                new_story = story_form.save()
                .....
            else:
                story_form = StoryForm(user=request.user)
                ....
    

    I also changed my initial code a bit.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am writing an app with both english and french support. The app requests

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.