Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Model Forms Display a Blank Model Form

Devin Reeh
PLUS
Devin Reeh
Courses Plus Student 6,314 Points

Django Forms Challenge Task

We've made models and model forms, now we need a view to render the form. Write a new view named product_form that instantiates the DigitalProductForm and provides it to the products/product_form.html template as the key "form".

I know that I'm supposed to instantiate the model in the view, but I don't think I'm doing it right. Please help with an explanation to your code pretty please! Or maybe a link to a vid where they explain what I'm doing wrong if you know. Thank you so much! Much appreciated.

products/views.py
from django.shortcuts import render

from . import forms

def product_form(request):
    form = forms.DigitalProductsForm()
    if request.method = 'POST':
        form = forms.digitalProductsForm(request.POST)
        if form.isValid():
products/templates/products/product_form.html
<form action='' method='POST'>
    <input type="submit" value="Save">
</form>

4 Answers

Don't overthink it too much.

from django.shortcuts import render

from . import forms

def product_form(request):
    form = forms.DigitalProductForm()
    return render(request, 'products/product_form.html', {'form': form})
Calum Devitt
Calum Devitt
3,763 Points

im stuck on this one too

I had trouble as well. The question is not asking you to check if it is "POST" or if it is valid. You have instantiated the form, now render it.

Ker Zhang
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ker Zhang
Full Stack JavaScript Techdegree Graduate 29,113 Points

def product_form(request): form = forms.DigitalProductForm() return render(request, 'products/product_form.html', {'form':form})

this worked for me.