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))

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to