美文网首页
[Django]Create a hello-world App

[Django]Create a hello-world App

作者: SharlotteZZZ | 来源:发表于2018-09-18 04:57 被阅读0次

    Versions:

    • Python 3.5.2
    • django 1.11.3

    Step 1 Create your folder and python file
    .../hello-world/hello.py

    Step 2 Three Required Django Settings

    import sys
    from django.conf import settings 
    
    settings.configure(
        DEBUG=True,
        SECRET_KEY='thisisabadkeybutitwilldo',
        ROOT_URLCONF=sys.modules[__name__],
    )
    
    • DEBUG - if True, Django will render helpful error pages in your browser. If set to False, those error pages will be hidden and a more generic error message will rendered.

    • SECRET_KEY - used to provide cryptographic signing, and should be set to a unique, unpredictable value.

    • ROOT_URLCONF - a pointer to the file containing the URL configuration rules. In normal project this is a separate file containing a series of rules for each page in our application. In the example below we're assigning the value sys.modules[name] which basically says the URL configurations will be in this file.

    Step 3 Configure the URL

    urlpatterns variable is a list of URL patterns. Here, we're creating just one pattern, /hello-world/

    from django.conf.urls import patterns
    urlpatterns = patterns('',
        (r'^hello-world/$', index),
    )
    

    Django1.10以后的版本已经不再引入patterns了

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

    Step 4 Configure the View

    You'll notice that after the /hello-world/ URL is the word index. That's the name of the view that we need to create. Views are Python functions (can be Python classes as well) that are executed when a certain URL is accessed. In our sample application we now need to create a Python function named index.

    from django.http import HttpResponse
    def index(request):
        return HttpResponse('Hello, World')
    

    Step 5 Enable Command Line Support

    Django comes with a utility called execute_from_command_line that can be used to run the development web server from the command line. In this step we'll add a little bit of code that will tell our application that if this file is the main script file, it should use the execute_from_command_line utility.

    from django.core.management import execute_from_command_line
    
    if __name__ == "__main__":
        execute_from_command_line(sys.argv)
    

    Putting all thing together, the hello.py should be like this:

    import sys
    from django.conf import settings
    from django.conf.urls import url
    from django.http import HttpResponse
    from django.core.management import execute_from_command_line
    
    settings.configure(
        DEBUG = True,
        SECRET_KEY = 'thisisabadkeybutitwilldo',
        ROOT_URLCONF = sys.modules[__name__],
    )
    
    def index(request):
        return HttpResponse('Hello, World')
    
    
    urlpatterns = [url(r'^hello-world/$', index, name='index'),]
    
    if __name__ == "__main__":
        execute_from_command_line(sys.argv)
    

    Last step -- Go to your terminal, navigate to the hello-world folder, and make sure the virtual env is activated. Then run the following command. When you run this command you'll see a warning message. You can ignore that for now.

    System check identified no issue
    September 05, 2018 - 16:02:52
    Django version 1.11.3, using settings None
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CTRL-BREAK.
    

    Go to a web browser and access this URL: http://127.0.0.1:8000/hello-world/. The worlds 'Hello, World' should display.

    相关文章

      网友评论

          本文标题:[Django]Create a hello-world App

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