How Django Works? - 03

Haebin Ethan Jeong·2020년 6월 7일
0

Django

목록 보기
3/4

Writing More Views

  • These views take an argument.

  • If we add this to urls.py,the browser accordingly returns different things.

  • For example, If we type "/polls/34/", it will run the detail() method and display "You're looking at question 34."

What do views.py do.

  • Each view is reponsible for 1. returning an HttpResponse object containing the content for the requested page, or 2. raising an exception such as Http404.

  • HttpResponse objects are your responsibility. Each view you write is responsible for instantiating, populating, and returning an HttpResponse. (It's like printing????)

Template for the website.

A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

  • Right now, the page's design is hard-coded in the view. In order to change the way the page looks, you need to create a directory called templates in your polls directory.
    Your project’s TEMPLATES setting describes how Django will load and render templates
  • By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS.

Making the html files for the template

  • We need to create a template at polls/templates/polls/index.html:

Updating polls/views.py for template

  • That code loads the template called polls/index.html and passes it a context.

Shortcut: render()

  • render() combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text.

  • Note that once we’ve done this in all these views, we no longer need to import loader and HttpResponse.

  • The render() function takes the request object as its first argument, a template name as its second argument and a dictionary as its optional third argument. It returns an HttpResponse object of the given template rendered with the given context.

Raising a 404 error

  • The view raises the Http404 exception if a question with the requested ID doesn’t exist.

Shortuct: get_object_or_404()

  • get_object_or_404() method: Calls get() on a given model manager, but it raises Http404 instead of the model’s DoesNotExist exception.
    - get() method returns the object matching the given lookup parameters.
  • The get_object_or_404() function takes a Django model as its first argument and an arbitrary number of keyword arguments, which it passes to the get() function of the model’s manager. It raises Http404 if the object doesn’t exist.

Using the template system

  • The template system uses dot-lookup syntax to access variable attributes. In the example of {{ question.question_text }}, first Django does a dictionary lookup on the object question. Failing that, it tries an attribute lookup – which works, in this case. If attribute lookup had failed, it would’ve tried a list-index lookup.

  • Method-calling happens in the {% for %} loop: question.choice_set.all is interpreted as the Python code question.choice_set.all(), which returns an iterable of Choice objects and is suitable for use in the {% for %} tag.

  • question.choice_set.all은 해당 Question모델을 foreign key로 참조하는 모든 Choice 모델을 가져온다. forloop.counter는 for문이 얼마나 돌았는지 1부터 시작하여 1씩 증가하여 반환한다.

Removing hardcoded URLS in templates

  • In polls/index.html: we have a hardcode like this: <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
  • And, this is how you remove hardcodes:
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
  • The way this works is by looking up the URL definition as specified in the polls.urls module. You can see exactly where the URL name of ‘detail’ is defined below:
  • If you want to change the URL of the polls detail view to something else, perhaps to something like polls/specifics/12/ instead of doing it in the template (or templates) you would change it in polls/urls.py:

Namespacing URL names

In order to differentiate different apps, we add namespaces to your URLconf.

  • In the polls/urls.py file, go ahead and add an app_name to set the application namespace:

profile
I'm a Junior studying Economics and Computer Science at Vanderbilt University.

0개의 댓글