Day41 - Continue the Django journey

    I didn't code the rhythm with the teacher today, because I definitely couldn't keep up. I like to understand the logic and all the knowledge involved in it while coding, so it is especially easy to go wrong and fall behind. Today, I finally caught up with the pure listening, and it was quite enjoyable to listen to, so I understood it. Evening self-study and typing more code, deepen understanding. Today I finally figured out mvc and mvt. Mvc is the standard mode followed by excellent web frameworks, while Django has its own unique mode, mvt, and still follows mvc, but c is handled by the framework itself, and many Ts refer to templates, that is, models. .

    First, start the virtual environment:

    Create a new project in the virtual environment:

So the following folders and files are generated:

     

The settings file already comes with a lot of Django commands and type methods. Leave it alone and modify the time zone of settings:

Confirm that the Django development server is running:

Create a new view.py file in the main project directory and enter the code:

from django.http import HttpResponse
import datetime


def hello(request):
    return HttpResponse('Hello, world!')


def current_datetime(request):
    now = datetime.datetime.now()
    html = 'It is now %s.' % now
    return HttpResponse(html)

Import the modules HttpResponse, datetime, create a view function named hello, pass the parameter named request, and return the sentence 'helloworld' instantiated by HttpResponse; then create a function that is convenient for calculating the date, just like hello, returns a string called html HttpResponse instantiated object

    Then, after modifying the url file, the content of the view can be displayed on the web page, and Django knows which url to call the view view file.

from django.conf.urls import url, include
from django.contrib import admin
from newsite.views import hello, current_datetime


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello/$', hello),
    url(r'^time/$', current_datetime),
]

Made two changes to the url configuration. First, import the hello, current_datetime functions at the top; second, add two URL patterns that map hello and time/url to that new view. This is the interface, the API! At the same time, this is called loose coupling! The components of these urls can be changed at any time, with little or no impact on the view file. The situation of url and view is called loose coupling. Huhu, finally figured it out!


Write these first today, and make up the exercises tomorrow. There is less code typing, and there are too many problems when doing practical exercises. We must slowly find the reason and debug it.




   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324864211&siteId=291194637