Django create application

Create an application: take the helloworld directory as an example

1. Enter the same level directory of manage.py

2. Type the command python manage.py startapp blog //blog is the name of the app, take whatever you want

3. Add the application name to INSTALLED_APPS in settings.py of helloworld

4. Write python code in views.py in the blog

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse

def  index(request):
    return HttpResponse("Hello World")

5. Configure the url and edit the urls.py file of HelloWorld

In the url function, the first parameter is not the url itself, the second is the response function, and the third is the url name

"""helloworld URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path

import  blog.views as bv

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', bv.index),
]

5. Start the server, python manage.py runserver

Access via http://127.0.0.1:8000/index/



Guess you like

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