Let's display a simple template that is ideal for Django for the first time

How to get used to Django

In the first place, I think the problem is that it's too difficult to understand SQL or something. Suddenly connect to the database and apply Untara Kantara ... Personally, I felt that the threshold was too high.

As I did in bottle for the first step, I thought that it might be from creating a template and displaying HTML, so I will summarize what I did.

Continuation from the last time

>http://qiita.com/Gen6/items/1848f8b4d938807d082e

First, let's create a directory to store the template. Create a directory called templates directly under mysite /.

I tried to capture the screen in an easy-to-understand manner.

スクリーンショット 2016-10-21 15.19.37.png

I will make the basic settings immediately.

Add description to the configuration file etc.

myapp/urls.py


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^template/$', views.index, name='index'), 
]

mysite/settings.py


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),], #Add here
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

templates/index.html


<!DOCTYPE html>
<html>
 <head lang="ja">
    <meta charset="UTF-8">
    <title></title>
 </head>
 <body>
  <p>welcome template</p>
 </body>
</html>

myapp/views.py


from django.http.response import HttpResponse
from django.shortcuts import render


def index(request):
    return render(request,'index.html')

It's OK if you can do it so far.

$ cd Djangoproject
$ source virtualenv/bin/activate
$ cd mysite
$ python manage.py runserver

http://127.0.0.1:8000/myapp/template/

If index.html is displayed in, it is complete.

In addition, there is also a method called inheritance for templates, so in that case, create main.html in the templates directory as follows.

templates/main.html


<!DOCTYPE html>
<html>
  <head lang="ja">
    <meta charset="UTF-8">
    <title></title>
  </head>
  <body>
    {% block body %}
    {% endblock %}
  </body>
</html>

And index.html is rewritten as follows and completed.

templates/index.html


{% extends "main.html" %}
{% block body %}
  <p>welcome template</p>
{% endblock %}

Recommended Posts

Let's display a simple template that is ideal for Django for the first time
Start Django for the first time
Let's try Linux for the first time
Register a task in cron for the first time
Kaggle for the first time (kaggle ①)
I want to create a lunch database [EP1-4] Django study for the first time
Kaguru for the first time
Summary of stumbling blocks in Django for the first time
What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume
[For self-learning] Go2 for the first time
See python for the first time
Let's write a simple simulation program for the "Monty Hall problem"
The first time a programming beginner tried simple data analysis by programming
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
I tried tensorflow for the first time
Django ~ Let's display it in the browser ~
A useful note when using Python for the first time in a while
Write a short if-else for Django Template
Looking back on the machine learning competition that I worked on for the first time
I tried using scrapy for the first time
How to use MkDocs for the first time
Make a histogram for the time being (matplotlib)
[Note] Deploying Azure Functions for the first time
I tried python programming for the first time.
A program that searches for the same image
I tried Mind Meld for the first time
virtualenv For the time being, this is all!
Try posting to Qiita for the first time
It's okay to participate for the first time! A hackathon starter kit that you want to prepare "before" participating in the hackathon!
Import audit.log into Splunk and check the behavior when Splunk is started for the first time
What is a rational decision that maximizes the chances of encountering an "ideal home"?
I made a command to wait for Django to start until the DB is ready
What I got into Python for the first time
I tried Python on Mac for the first time.
Linux is something like that in the first place
I tried python on heroku for the first time
For the first time, I learned about Unix (Linux).
DJango Note: From the beginning (creating a view from a template)
Make the model a string on a Django HTML template
Creating a list when the nomenclature is a fixed time
AI Gaming I tried it for the first time
Launch a simple WEB server that can check the header
Let's make a robot that solves the Rubik's Cube! 2 Algorithm
Let's make a robot that solves the Rubik's Cube! 3 Software
Display the time in Django according to the user's region (UTC)
Let's make a robot that solves the Rubik's Cube! 1 Overview
Is there a bias in the numbers that appear in the Fibonacci numbers?
[Django] A collection of scripts that are convenient for development
The image display function of iTerm is convenient for image processing.
I tried the Google Cloud Vision API for the first time
(For beginners) Try creating a simple web API with Django
I want to create a Dockerfile for the time being.
If you're learning Linux for the first time, do this!
Hypothesis / Verification (176) How to make a textbook that is easier than "The easiest textbook for quantum computers"
Code that I wish I had remembered when I participated in AtCoder for the first time (Reflection 1 for the next time)
The story of releasing a Python text check tool on GitHub x CircleCI for the first time
I made a function to check if the webhook is received in Lambda for the time being
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
Introduction to Deep Learning for the first time (Chainer) Japanese character recognition Chapter 3 [Character recognition using a model]
The story of returning to the front line for the first time in 5 years and refactoring Python Django
What I learned by writing a Python Pull Request for the first time in my life