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

Fahad Almutairi
Fahad Almutairi
12,079 Points

DJANGO CBVS CRUDS challenge - didn't find a correct template for `ArticleCreate`

views.py

class ArticleUpdate(UpdateView):
    model = models.Article
    template_name = 'articles/article_form.html'
    fields = ('title', 'body', 'author', 'published')

class ArticleCreate(CreateView):
    model = models.Article
    fields = ('title', 'body', 'author', 'published')
    template_name = 'articles/article_form.html'

In article_form.html

  <form method='POST'>
    {% csrf_token %} {{form.as_p}}
    <input type='submit' value='Submit'>
  </form>

I get "Hmm, didn't find a correct template for ArticleCreate". I tried changing the templates path and it didn't work.

For some reason the help button within the challenge won't work and I don't know how to post the question to the relevant section.

Challenge: "Alright, now to do the article_form.html template. This template will serve both the update and create views. You can make it generic for now, though, and just include the form and a submit button.

Don't forget the CSRF token!"

2 Answers

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

Hello, you don't need to specify template_name in your views, because default will be article_form.html in this case. What you've missed is {% block body_content %}.

so your template should look more like this

{% block body_content %}
<form method='POST'>
  {% csrf_token %}
  {{ form.as_p }}
  <input type="submit" class="btn btn-primary" value="Save">
</form>
{% endblock %}
Fahad Almutairi
Fahad Almutairi
12,079 Points

It worked but I don't see why I need a block when the template is not extending anything. Isn't that the purpose of blocks?

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

You're right that actually is the purpose of blocks. To be honest I feel like it should be working without that block as well. My guess is that checks that are performed to validate the code challenge validate the some page and this part is inserted there, so maybe it needs that block to actually be inserted there