On the basis of the HelloWorld project, we are going to create the first web-based Django application, name the app the demo first, how to create the application package, of course, the first choice we can go into the cmd, execute the following command
python manage.py startapp demo
If you want to do it in Pycharm: or to go into the edit configuration in the inside to configure: To change the demo to another name, is to add other applications, execute this command and go into the cmd inside to execute the script is the same execution is visible: Next, OK, let's write the first MVC "View" layer below demo/views.py, the original code is:
from django.shortcuts import render
# Create your views here.
OK, change to:
from django.http importHttpResponse
def index(request):
returnHttpResponse("Hello, world. You‘re at the demo index.")
The meaning of this view is that after receiving an HTTP request, return a sentence of response, it can be displayed on the Web page, this is the simplest view layerUnder demo/url.py, add the following code:
from django.conf.urls import url
from.import views
urlpatterns =[
url(r‘^$‘, views.index, name=‘index‘),
]
Change HelloWorld's url.py to:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns =[
url(r‘^demo/‘, include(‘demo.urls‘)),
url(r‘^admin/‘, admin.site.urls),
]
Then start the service, access the http://localhost:8081/demo/, that is, visible: So far, the first view has been successfully built, then look at the URL method:
url(r‘^$‘, views.index, name=‘index‘),
The first parameter is a prerequisite, that is, the second argument of the regular expression is also a required parameter, pointing to a method that passes the HttpRequest object and two parameters Kwargs and name, one for passing parameters, one for the name, and not for the required parameters.
Python Learning note--2, creating the first Django app