Making templates

In the final section of this chapter, we will see how PyCharm supports the process of making Django templates within a web project. To see the power of these features in action, we will be extending the index view of our current library application.

  1. Open the library/views.py file and modify its content to the following:
from django.shortcuts import render
from .models import Book


# Create your views here.
def index(request):
latest_books = Book.objects.order_by('-pub_date')[:5]
context = {'latest_books': latest_books}
return render(request, 'library/index.html', context)

Here we are creating a dynamic view for our library application. Specifically, we access the Book database table and retrieve the latest five entries, according to their publication date (the pub_date attribute in reverse, hence the minus sign).
Finally, we send that information to a template named library/index.html via the render() function.

  1. Notice that there is now a warning within the current library/views.py file, saying that the template library/index.html we specified does not exist:
Django template not found warning

If you have worked with Django before, you know that we now need to manually create this file within the templates folder of our web project. However, since we are using PyCharm, things are much simpler!

  1. With your cursor at the error, click on the corresponding intention action icon (the yellow lightbulb to the left), and select the first option from the drop-down list to create the template in question:
Django-specific intention actions
  1. Choose to create a new folder in the templates folder in the pop-up window if prompted, and, after the process, the library/index.html template should be created accordingly. It will also be opened in the editor automatically.
  1. Now, enter the following code for our template, which lists all the entries we currently have in our Book database table:
{% if latest_books %}
<ul>
{% for book in latest_books %}
<li>{{ book }}</li>
{% endfor %}
</ul>
{% else %}
<p>No books available.</p>
{% endif %}

Again, I highly recommend manually writing the code yourself, so that you can experience PyCharm's excellent code-completion features. In this case, we see that code completion is able to support HTML and Jinja syntax, making writing Django templates an effortless process.

  1. Now, either use the run configuration customized earlier, or run the server directly from the manage.py panel and go to the /library page. Here, you should see the corresponding output that we specified in our views and templates—a list of entries from our Book database table, printed out via the __str__() method of the Book model.

The last element of note while working on a Django project with PyCharm is the convenient navigation between views and templates. Specifically, notice the icons at the top of the gutter of our current views and templates:   in the templates/library/index.html file, and   in the library/views.py file.

As we know, one is a Django view that utilizes another, which is a template. These two kinds of files are therefore interconnected in their logic and are likely to be worked on concurrently by Django developers generally. Via the aforementioned icons, you can jump back and forth between a view and its corresponding template with a mouse click, which will undoubtedly prove considerably useful.

And that concludes our current discussion on the PyCharm support for Django applications. Note that there are a number of other great features that we can explore, and we will come back and further expand on this topic later in Chapter 10, Building a Web Application in PyCharm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset