Django for Designers/Basic views: Difference between revisions

imported>Aldeka
No edit summary
 
(8 intermediate revisions by 4 users not shown)
Line 2:
 
<!-- Instructor git note: You should do the above, and then do: git push origin HEAD:pre-part-2 -->
 
<div class="instructor">Time: 20 minutes</div>
 
=== Note for tutorial attendees ===
Line 23 ⟶ 25:
<div class="instructor">For Django, a ''model'' represents how the data is stored. This is the part of your app that ties your data to storage in a database. You write Python code, and Django converts it and out of into SQL, the language spoken by databases.
 
A ''view'' in Django handles a ''request'' from the website visitor for a web page. What you do with this request is up to you, and is the domain of the view code that you write. For example, when a request comes in, you could send start a background process to send a fax to Tokyo indicating how happy you are that someone came to your website. Or you could provide a HTTP response to the website visitor with a list of recently bookmarked web pages if you are providing a bookmarking app.
 
The ''templates'' in Django control how data is presented. Typically, a view provides a collection of data to the template. The template then might loop over that information, wrap it in HTML bulleted lists, wrap ''that'' in a standard layout for all pages across your site, and serve that out to the site visitor. The templates usually refer to some ''static'' content, such as CSS, that makes the site actually seem designed!
Line 56 ⟶ 58:
url(r'^$', 'bookmarks.views.index', name='home'),
url(r'^bookmarks/$', 'bookmarks.views.index', name='bookmarks_view'),
url(r'^tags/([\w-]+)/$', 'bookmarks.views.tag'),
)
)</source>
 
Suppose a visitor to your site goes to (if you view the page, don't worry about the ViewDoesNotExist message, we'll get to that in a minute) [http://localhost:8000/tags/awesome/ http://localhost:8000/tags/awesome/].
* which regex pattern is tripped?
* what function is then called?
Line 189 ⟶ 192:
 
Now if you go to http://localhost:8000, you should see the webpage we made in our template file!
 
If you do not, you may need to '''restart your development server''' before it "sees" your template.
 
Save and commit your new template and template-based view.
Line 301 ⟶ 306:
 
<div class="instructor">There are lots of built-in template tags and filters; we'll see a few more later. The full docs on them [https://docs.djangoproject.com/en/dev/ref/templates/builtins/ live here]. </div>
 
Reload your webserver (if necessary) and take a look at http://localhost:8000 and http://localhost:8000/tags/awesome/ . They're both based on templates now -- and based on the same base template, so any changes we make to base.html will appear in both automatically!
 
As a rule, in Django, things in {% %} control the "logic" of the template, while things in {&#8288;{ }} actually stick data into the template in particular places.
Line 331 ⟶ 338:
 
Make sure you see ''manage.py'', and then let's continue.
 
TODO (Karen): see how the old HTML looks here
 
Now let's add some more structure to our HTML templates to make them easier to style. First, base.html:
Line 397 ⟶ 402:
<div id="new-bookmark-widget">
<form method="post">
<h3>New bookmarkBookmark</h3>
<p><label>URL</label> <input></p>
<p><label>Title</label> <input></p>
Line 432 ⟶ 437:
Save and commit your work!
 
[[Django_for_Designers/Adding_models|Next page]]
<!-- Teacher git note: git push origin HEAD:pre-part-3 -->
Anonymous user