Skip to content

Broken person_create #2

@CapnKernel

Description

@CapnKernel

Reference: http://melbdjango.github.io/lesson-five/#/32 "Improvements"

def person_create(request):
    person = get_object_or_404(Person, pk=pk)

This isn't going to work: Where does pk come from? Also, there's a comment # Create person instance but no instance is created.

I don't know real programmers do it, but for better or for worse, my code would have a person_create() and a person_edit(). person_edit() takes a pk, and the code is as in the slide (except for something else I mention below).

My person_create() code would look like this:

def person_create(request):
    if request.method == 'POST':
        form = HelloWorldForm(request.POST)
        if form.is_valid():   
            # Create person instance
            person = Person()
            …

Here's the other problem with a person_edit with the code as per the slide: If validation fails, we want to redisplay the form, but creating the form with form = HelloWorldForm() misses out on being seeded with the form data. I'd change it to:

        form = HelloWorldForm(initial={'name': person.name, 'awesome: person.awesome})

Putting this here to make the slides even more awesomier for the next intake.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions