0

I have a user model for which I'm trying to make a view that manages both create/update form rendering/post.

Here is the view that I did for now

def user_edit(request, user_id=None):
    obj = {}
    status = 200

    if user_id:
        user = get_object_or_404(User, pk=user_id)
    else:
        user = User()

    user_form = UserForm(instance=user, prefix='user')

    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=user, prefix='user')
        if user_form.is_valid():
            user_form.save()
        else:
            status = 406

    obj['user_form'] = user_form
    return render(request, 'user/edit.html', obj, status=status)

This works fine, but as you can see, my user_form is initialized 2 times. In order to make this more DRY, at POST time I'd like to update the form definition instead of redefining it. Something like:

    if request.method == 'POST':
        user_form.data = request.POST
        user_form.prefix = 'user'

But I can't make this work. So 2 questions:

  • Does my view seem valid ?
  • How can I avoid the form re-definition ?

2 Answers 2

3

I would just restructure a couple of lines this way:

def user_edit(request, user_id=None):

    status = 200

    if user_id:
        user = get_object_or_404(User, pk=user_id)
    else:
        user = User()

    user_form = UserForm(request.POST or None, instance=user, prefix='user')

    if request.method == 'POST':
        if user_form.is_valid():
            user_form.save()
        else:
            status = 406

    return render(request, 'user/edit.html', {'form': user_form}, status=status)

Sometimes, it makes sense to duplicate may be 1 line of code to keep it readable.

Sign up to request clarification or add additional context in comments.

1 Comment

For the record I also tried user_form = UserForm(request.POST, instance=user, prefix='user'): No error but that always returns an empty form.
0

You should use if condition like this to initialize form only once:

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render(request, 'contact.html', {
        'form': form,
    })

Taken from documentation of django. If you are new to Python, it may seem strange to define a variable in if..else statement, but it is pretty common and valid way in Python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.