Newbie question. I have an application where registered users can submit 
stories. The posting and display works great but I wanted users to have the 
ability to edit any of their stories. The goal would be for a user to find 
a list of their stories on their profile page, choose one that they would 
like to edit, and then retrieve the form they initially submitted with the 
current information filled in to make the necessary edits.

I composed a view, url and template to achieve this. The page displays, the 
story number from the url appears in the address bar but the form is 
nowhere to be found. I can't figure out if what I'm doing is an error in 
the template or the view (I'm thinking the former but can't be sure).

Here is the model:

class Story(models.Model):
    objects = StoryManager()
    title = models.CharField(max_length=100)
    topic = models.CharField(max_length=50)
    copy = models.TextField()
    author = models.ForeignKey(User, related_name="stories")
    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)
    pic = models.ImageField(upload_to='pictures', blank=True)
    caption = models.CharField(max_length=100)

    def __unicode__(self):
        return " %s" % (self.title)

Here is the form:

class StoryForm(forms.ModelForm):
    class Meta:
        model = Story
        exclude = ('author',)

    def __init__(self, author, *args, **kwargs):
        super(StoryForm, self).__init__(*args, **kwargs)
        self.author = author


    def save(self, commit=True):
        self.instance.author = self.author
        return super(StoryForm, self).save(commit)

Here are the submit and edit views:

@login_required
def submit_story(request):
    story_form = None
    if request.method =="POST":
        story_form = StoryForm(request.user, data=request.POST, 
files=request.FILES)
        if story_form.is_valid():
            new_story = story_form.save(commit=True)
            return HttpResponseRedirect("/report/all/")

    return render_to_response("report/report.html", {'form': story_form or 
StoryForm(request.user) }, context_instance=RequestContext(request))

@login_required
def edit_story (request, story_id):
    story_form = None
    if story_id:
        story_form = get_object_or_404(Story, pk=story_id)
        if story_form.author != request.user:
            return HttpResponse("You can only edit your own stories!")

    if request.method == 'POST':
        story_form = StoryForm(request.user, data=request.POST, 
files=request.FILES)
        if story_form.is_valid():
            story_form.save(commit=True)
            return HttpResponse("/profiles/user_profile")
    #else:
        #story_form = StoryForm(instance=story_form)

    return render_to_response('report/storyedit.html', {'form': story_form 
or StoryForm(request.user) }, context_instance=RequestContext(request))


The urls:

    url(r'^report/$', 'project.report.views.submit_story', 
name='write_story'),
    url(r'^detail/(?P<story_id>\d*)/edit/$', 
'project.report.views.edit_story', )

And the edit template:

{% extends 'base.html' %}
{% load report %}
{% block page_title %}Edit Story{% endblock %}
{% block headline %}Edit Story{% endblock %}
{% block content %}

    <div class="row">
        <div class="span12">
            <div class="page-header">
                <h2>Edit your story, <a href="{% url 
profiles_profile_detail user.username %}">{{user.username}}!</a></h2>
            </div>

            <form action="" class="form form-horizontal" method="post" 
enctype="multipart/form-data">

                <fieldset>
                    {% generic_form form %}
                    <div class="form-actions">
                        <input type="submit" value="Submit" class="btn 
btn-primary"/>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
{% endblock %}


This is likely a stupid error but hours of scratching my head led me to 
seek some guidance. Any help welcomed and appreciated. Trying very hard to 
learn.


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/arNGcd_FldoJ.
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