简体   繁体   English

服务器无法在hello world应用程序中启动

[英]Server doesn't start in hello world app

I am trying to create a Django hello world app. 我正在尝试创建一个Django hello world app。

I have the following code: 我有以下代码:

import sys

from django.conf import settings
from django.http import HttpResponse

settings.configure(
    DEBUG=True,
    SECRET_KEY='badkey',
    ROOT_URLCONF=sys.modules[__name__],
)

def index(request):
    return HttpResponse('Hello, World')

urlpatterns = [
    (r'^hello-world/$', index),
]

Located at /smartQuiz/smartQuiz/hello.py , where smartQuiz is the root directory of my project. 位于/smartQuiz/smartQuiz/hello.py ,其中smartQuiz是我项目的根目录。

If I run python hello.py runserver from the directory where hello.py is located, the server won't start. 如果我跑python hello.py runserver从所在目录hello.py所在,服务器将无法启动。

What am I doing wrong? 我究竟做错了什么?

You seem to be missing 你似乎失踪了

 from django.conf.urls import url

Here is a full minimal example previously written by minigunnr on the Stack Overflow documentation: 以下是minigunnr以前在Stack Overflow文档中编写的完整最小示例:

This example shows you a minimal way to create a Hello World page in Django. 此示例显示了在Django中创建Hello World页面的最小方法。 This will help you realize that the django-admin startproject example command basically creates a bunch of folders and files and that you don't necessarily need that structure to run your project. 这将帮助您了解django-admin startproject示例命令基本上创建了一堆文件夹和文件,并且您不一定需要该结构来运行您的项目。

  1. Create a file called file.py. 创建一个名为file.py的文件。

  2. Copy and paste the following code in that file. 将以下代码复制并粘贴到该文件中。

     import sys from django.conf import settings settings.configure( DEBUG=True, SECRET_KEY='thisisthesecretkey', ROOT_URLCONF=__name__, MIDDLEWARE_CLASSES=( 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ), ) from django.conf.urls import url from django.http import HttpResponse # Your code goes below this line. def index(request): return HttpResponse('Hello, World!') urlpatterns = [ url(r'^$', index), ] # Your code goes above this line if __name__ == "__main__": from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) 
  3. Go to the terminal and run the file with this command python file.py runserver . 转到终端并使用此命令python file.py runserver运行该文件。

  4. Open your browser and go to 127.0.0.1:8000. 打开浏览器并转到127.0.0.1:8000。

I see that you are trying to directly call hello.py with runserver as an argument. 我看到你试图用runserver直接调用hello.py作为参数。 If you are writing and trying to test Django, make sure that you are using the framework to start the server. 如果您正在编写并尝试测试Django,请确保使用该框架来启动服务器。 It will take care of all the 'Django stuff' required: python manage.py runserver manage.py takes care of setting things up for you. 它将处理所需的所有'Django东西': python manage.py runserver manage.py负责为您设置。

I think you are doing it the wrong way. 我认为你这样做是错误的。 Use this link to know how to make a django app 使用此链接了解如何制作django应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM