python django 404错误

时间:2017-08-19 17:34:46

标签: python django

我正在学习python django。但我收到此错误

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/

Using the URLconf defined in x.urls, Django tried these URL patterns, in this order:

^admin/
^y/

The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我真的不知道为什么我会收到这个错误 首先我创建了django项目,如:

   django-admin startproject x

然后我收到了这些文件:

   db.sqlite3  manage.py  x

然后我创建了app:

   python manage.py startapp y

然后我收到了这些文件:

   db.sqlite3  manage.py  x  y

然后我在我的x文件夹中编辑了urls.py并包含了我在y文件夹中创建的文件代码 我在x文件夹中的urls.py文件

  from django.conf.urls import include, url
  from django.contrib import admin

  urlpatterns = [
              url(r'^admin/', admin.site.urls),
              url(r'^y/',include('y.urls'))
  ]

我在y文件夹中的urls.py文件

  from django.conf.urls import url
  from . import views

  urlpatterns = [
            url(r'^$/', views.index, name = 'index'),

  ]

我的文件夹y中的view.py文件是

     from django.http import HttpResponse


     def index(request):
                return HttpResponse("<h1>hello sir</h1>")

当我在终端开始它时,它开始很好:

     python manage.py runserver
     Performing system checks...

     System check identified no issues (0 silenced).
     August 19, 2017 - 17:32:10
     Django version 1.11.4, using settings 'x.settings'
     Starting development server at http://127.0.0.1:8000/
     Quit the server with CONTROL-C.

这些是我下载pycharm以编译和运行python的额外信息 在那里我测试了我的上述文件y / urls.py x / urls.py

当我编译y / urls.py文件时出现此错误:

  /usr/bin/python2.7 /home/peter/Desktop/x/x/urls.py
  Traceback (most recent call last):
  File "/home/peter/Desktop/x/x/urls.py", line 5, in <module>
  url(r'^admin/', admin.site.urls),
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/admin/sites.py", line 280, in urls
   return self.get_urls(), 'admin', self.name
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/admin/sites.py", line 238, in get_urls
   from django.contrib.contenttypes import views as contenttype_views
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/contenttypes/views.py", line 5, in <module>
    from django.contrib.contenttypes.models import ContentType
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/contenttypes/models.py", line 139, in <module>
    class ContentType(models.Model):
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/contenttypes/models.py", line 140, in ContentType
    app_label = models.CharField(max_length=100)
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/db/models/fields/__init__.py", line 1061, in __init__
    super(CharField, self).__init__(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/db/models/fields/__init__.py", line 172, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
     File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/conf/__init__.py", line 56, in __getattr__
     self._setup(name)
     File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
    django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

   Process finished with exit code 1

并在编译x / urls.py文件时遇到此错误:

  /usr/bin/python2.7 /home/peter/Desktop/x/x/urls.py
   Traceback (most recent call last):
   File "/home/peter/Desktop/x/x/urls.py", line 5, in <module>
   url(r'^admin/', admin.site.urls),
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/admin/sites.py", line 280, in urls
   return self.get_urls(), 'admin', self.name
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/admin/sites.py", line 238, in get_urls
   from django.contrib.contenttypes import views as contenttype_views
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/contenttypes/views.py", line 5, in <module>
   from django.contrib.contenttypes.models import ContentType
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/contenttypes/models.py", line 139, in <module>
   class ContentType(models.Model):
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/contrib/contenttypes/models.py", line 140, in ContentType
   app_label = models.CharField(max_length=100)
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/db/models/fields/__init__.py", line 1061, in __init__
   super(CharField, self).__init__(*args, **kwargs)
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/db/models/fields/__init__.py", line 172, in __init__
   self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/conf/__init__.py", line 56, in __getattr__
   self._setup(name)
   File "/usr/local/lib/python2.7/dist-packages/Django-1.11.4-py2.7.egg/django/conf/__init__.py", line 39, in _setup
   % (desc, ENVIRONMENT_VARIABLE))
   django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

   Process finished with exit code 1

我还编译了views.py和setting.py,但是他们成功编译没有任何错误但只在x / urls.py和y / urls.py文件中出错只能帮帮我 但是当我打开网页浏览器时,我收到了错误。

我只是想在我的网络浏览器上打印你好先生,但得到这个错误不知道为什么。我使用的是firefox的更新版本我在最新版本的Chrome中试过这个但是不知道为什么请帮我,请教我如何解决这个问题

我在互联网的帮助下独自学习程序,所以请帮我解决这个错误 对不起,我的英语很糟,请不要介意语法错误,请帮助我 请...

请帮助我,请教我如何解决此错误

1 个答案:

答案 0 :(得分:0)

r'^$/'删除斜杠。美元$表示字符串结束,因此如果您在其后添加任何内容,那么您将永远不会得到匹配项。

然后,您应该在浏览器中尝试127.0.0.1:8000/y/。您尚未在项目的网址中为^$定义模式,因此127.0.0.1:8000/无效。