Best practices for Django views.py and urls.py (?)

Before making a simple application with Django

How can I learn around the settings of views.py and urls.py in a very easy way? I thought that if I suppressed only the basics before repeating many mistakes, the rest would be smooth.

The introduction of Django is a past article http://qiita.com/Gen6/items/1848f8b4d938807d082e

It is assumed that the template setup has been completed as this time. http://qiita.com/Gen6/items/a5562c36fc5c67c89916

I think that the story will be easier to understand if you do the same by referring to past articles.

Let's display the time

templates/index.html


{% extends "base.html" %}
{% block body %}

    <p>{{hour}}It's time</p>

{% endblock %}

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'),
]

myapp/views.py


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


def index(request):
    Datetime = {
        'hour': datetime.now().hour,
    }
    return render(request, 'index.html', Datetime)

If you check the above at http://127.0.0.1:8000/myapp/, the time should be displayed. If the time zone is out of order, change from'UTC'to'Asia / Tokyo'in settings.py.

Try to make a form that returns the contents after entering the name

templates/index.html



{% extends "base.html" %}
{% block body %}

    <form action="" method="get">
      <label>name:<input type="text" size="20" name="your_name"></label>
      <input type="submit" value="Send">
    </form>
    {% if your_name %}
      <p>{{ your_name }}Hey,.</p>
    {% endif %}

{% endblock %}

myapp/urls.py


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

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

myapp/views.py


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


def get_query(request):
    Message = {
        'your_name': request.GET.get('your_name'),
    }
    return render(request, 'index.html', Message)

If you check the above at http://127.0.0.1:8000/myapp/, a form will appear, Returns the string entered in the form.

How to mix the two behaviors

Well, the main subject is from here. How can I make use of the form while displaying the time?

An example that does not work

templates/index.html


{% extends "base.html" %}
{% block body %}

    <p>{{hour}}It's time</p>
    <form action="" method="get">
      <label>name:<input type="text" size="20" name="your_name"></label>
      <input type="submit" value="Send">
    </form>
    {% if your_name %}
      <p>{{ your_name }}Hey,.</p>
    {% endif %}

{% endblock %}

myapp/urls.py


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

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

myapp/views.py


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


def index(request):
    Datetime = {
        'hour': datetime.now().hour,
    }
    return render(request, 'index.html', Datetime)

def get_query(request):
    Message = {
        'your_name': request.GET.get('your_name'),
    }
    return render(request, 'index.html', Message)

This will not work. Where is the problem?

Working example

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'),
]

myapp/views.py


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


def index(request):
    Message = {
        'your_name': request.GET.get('your_name'),
        'hour': datetime.now().hour
    }
    return render(request, 'index.html', Message)

I think that if you can grasp it, you can deepen your understanding by trying various settings.

The actual application creation is in a separate article. http://qiita.com/Gen6/items/735245423b65698428be

Recommended Posts

Best practices for Django views.py and urls.py (?)
__version__ traps and best practices
Best practices for dynamically handling LINE Flex Messages in Django
Best practices for messing with data with pandas
Django 1.9 for internationalization
Personal best practices for VS Code-fronted Python development environments
[For beginners] Django Frequently used commands and reference collection
Django --models.py and admin.py
DBSCAN practices and algorithms