urlhelper takes a second parameter, which is the method to send the request to. In the homepage example, if the URL matches an empty string, the request is routed to
theme.home. The definition for
theme.homelives in ubyssey/views/main.py:
contextand the final
ubyssey.cafolder so that all the files are in one place.
File > Add Project Folderand select the
homemethod is responsible for sending an HTML response back to the user, but before it can do that, it has to fetch and prepare all the data it needs. Most of the calls to
ArticleHelperfetch data from our database, which is then added to the
contextstructure at the end of the method.
contextdata structure is a primitive type called a
dictionary, which is just Python's version of a hash table. It simply creates a map of keys to values:
contextdata structure to include a random number from 1 to 10.
homemethod makes a call to the
rendermethod, which (you guessed it!) renders the final output of the homepage. You'll notice that it takes three arguments:
request- the Django request object, stores information about the incoming request
homepage/base.html- the template file to use
context- the context to pass to the template
rendermethod loads the
homepage/base.htmltemplate and returns the result of rendering that template with the given context.
templates/homepage/base.htmlso that it prints the following at the top of the page:
YOUR RANDOM NUMBER HERE