02 - Requests and responses

Requests and responses

  1. Create project and app

    # Creating a project
    $ django-admin startproject mysite
    # The development server
    $ python manage.py runserver [ip:port]
    # Creating the Polls app
    $ python manage.py startapp polls
    
  2. Write your first view

    # 在 polls/views.py 编写一个url请求的相应
    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("Hello, world. You're at the polls index.")
    
    # 给polls创建urls.py,设置urlpatterns
    # 设置polls/urls.py
    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('', views.index, name='index'),
    ]
    
    # 在mysite/urls.py 将 polls 的urls 包含进去
    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('polls/', include('polls.urls')),
        path('admin/', admin.site.urls),
    ]
    

你可能感兴趣的:(02 - Requests and responses)