Python Learning note--2, creating the first Django app

Source: Internet
Author: User

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
    1. 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:
    1. from django.shortcuts import render
    2. # Create your views here.
OK, change to:
    1. from django.http importHttpResponse
    2. def index(request):
    3. 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:
  1. from django.conf.urls import url
  2. from.import views
  3. urlpatterns =[
  4. url(r‘^$‘, views.index, name=‘index‘),
  5. ]
Change HelloWorld's url.py to:
  1. from django.conf.urls import include, url
  2. from django.contrib import admin
  3. urlpatterns =[
  4. url(r‘^demo/‘, include(‘demo.urls‘)),
  5. url(r‘^admin/‘, admin.site.urls),
  6. ]
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:
    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.