美文网首页
Tutorial_1:Request and Response

Tutorial_1:Request and Response

作者: longgb246 | 来源:发表于2017-04-23 16:43 被阅读0次

    一、创建一个Project

    django-admin startproject longgb246
    

    产生目录

    longgb246/
        manage.py
        mysite/
            __init__.py
            settings.py
            urls.py
            wsgi.py
    

    二、开启服务

    python manage.py runserver
    

    端口号:http://127.0.0.1:8000/
    还可以改变端口,或者IP

    python manage.py runserver 8080
    python manage.py runserver 0.0.0.0:8000
    

    三、开启app

    python manage.py startapp polls
    

    得到以下目录

    polls/
        __init__.py
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        tests.py
        views.py
    

    3.1 写第一个view

    打开polls/views.py
    写入

    from django.http import HttpResponse
    def index(request):
        return HttpResponse("Hello, world. You're at the polls index.")
    

    3.2 创建app的urls

    得到以下目录

    polls/
        __init__.py
        admin.py
        apps.py
        migrations/
            __init__.py
        models.py
        tests.py
        urls.py
        views.py
    

    urls.py中写到

    from django.conf.urls import url
    from . import views
    urlpatterns = [
        url(r'^$', views.index, name='index'),
    ]
    

    3.3 创建项目的urls

    在目录的urls.py
    写入

    from django.conf.urls import include, url
    from django.contrib import admin
    urlpatterns = [
        url(r'^polls/', include('polls.urls')),
        url(r'^admin/', admin.site.urls),
    ]
    

    把polls写入其中。

    相关文章

      网友评论

          本文标题:Tutorial_1:Request and Response

          本文链接:https://www.haomeiwen.com/subject/bvjyzttx.html