H a n d s o n, p r o j e c t b a s e d
Chapter 18 try it yourSelf
Download 4.21 Mb. Pdf ko'rish
|
Python Crash Course, 2nd Edition
- Bu sahifa navigatsiya:
- Making Pages: The Learning Log Home Page
394
Chapter 18 try it yourSelf 18-2. Short Entries: The __str__() method in the Entry model currently appends an ellipsis to every instance of Entry when Django shows it in the admin site or the shell. Add an if statement to the __str__() method that adds an ellipsis only if the entry is longer than 50 characters. Use the admin site to add an entry that’s fewer than 50 characters in length, and check that it doesn’t have an ellipsis when viewed. 18-3. The Django API: When you write code to access the data in your project, you’re writing a query. Skim through the documentation for querying your data at https://docs.djangoproject.com/en/2.2/topics/db/queries/. Much of what you see will look new to you, but it will be very useful as you start to work on your own projects. 18-4. Pizzeria: Start a new project called pizzeria with an app called pizzas. Define a model Pizza with a field called name, which will hold name values, such as Hawaiian and Meat Lovers. Define a model called Topping with fields called pizza and name. The pizza field should be a foreign key to Pizza, and name should be able to hold values such as pineapple, Canadian bacon, and sausage . Register both models with the admin site, and use the site to enter some pizza names and toppings. Use the shell to explore the data you entered. Making Pages: The Learning Log Home Page Making web pages with Django consists of three stages: defining URLs, writing views, and writing templates. You can do these in any order, but in this project we’ll always start by defining the URL pattern. A URL pattern describes the way the URL is laid out. It also tells Django what to look for when matching a browser request with a site URL so it knows which page to return. Each URL then maps to a particular view—the view function retrieves and processes the data needed for that page. The view function often ren- ders the page using a template, which contains the overall structure of the page. To see how this works, let’s make the home page for Learning Log. We’ll define the URL for the home page, write its view function, and create a simple template. Because all we’re doing is making sure Learning Log works as it’s sup- posed to, we’ll make a simple page for now. A functioning web app is fun to style when it’s complete; an app that looks good but doesn’t work well is pointless. For now, the home page will display only a title and a brief description. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling