简体   繁体   English

Django-尽管其他视图有效,但缺少LOCALHOST

[英]Django - LOCALHOST missing despite other views working

BACKGROUND : 背景

Worked through Django's tutorial and created multiple views: 遍历Django的教程并创建了多个视图:

  • /admin /管理员
  • /app / app

Currently having issues with "localhost:8000" despite localhost/app and localhost/admin views working (see below): 尽管存在localhost / app和localhost / admin视图,但当前仍存在“ localhost:8000”问题(请参见下文):

CODE

Folder Structure (virtualenv): 文件夹结构(virtualenv):

main/
   db.sqlite3
   manage.py
   app/
      _init_.py
      admin.py
      apps.py
      models.py
      tests.py
      views.py
      urls.py
      migrations/
         _init.py

   main/
      _init_.py
      settings.py
      urls.py
      wsgipy

../main/app/views.py: ../main/app/views.py:

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the app index.")

../main/app/urls.py: ../main/app/urls.py:

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

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

../main/main/urls.py: ../main/main/urls.py:

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

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

ISSUES/QUESTIONS: 问题/问题:

  1. localhost/app or localhost/admin shows a rendered page. localhost / app或localhost / admin显示呈现的页面。 However, "localhost:8000" does not show the original, django landing page (despite it working before). 但是,“ localhost:8000”不会显示原始的Django登录页面(尽管之前可以运行)。 What am I missing? 我想念什么?
  2. Do you recommend any other folder structure? 您是否建议其他文件夹结构? From my understanding, it's poor-form to put /main/main in the location that it currently is. 根据我的理解,将/ main / main放在当前位置的格式不正确。 Do you have recommendations on a new folder structure? 您对新的文件夹结构有建议吗?

localhost/app or localhost/admin shows a rendered page. localhost / app或localhost / admin显示呈现的页面。 However, "localhost:8000" does not show the original, django landing page (despite it working before). 但是,“ localhost:8000”不会显示原始的Django登录页面(尽管之前可以运行)。 What am I missing? 我想念什么?

It looks like main/main/urls.py is your global urls config, which would mean that localhost:8000 doesn't have a route defined (only localhost:8000/admin and localhost:8000/app .) You should put the url pattern in the global config. 看起来main/main/urls.py是您的全局url配置,这意味着localhost:8000没有定义路由(仅localhost:8000/adminlocalhost:8000/app 。您应该放置url全局配置中的模式。 You have defined url(r'^$', views.index, name='index') within app's scope, so Django is actually reading it with app/ prepended to it. 您已经在应用程序的范围内定义了url(r'^$', views.index, name='index') ,因此Django实际上是在app/读取它。

Do you recommend any other folder structure? 您是否建议其他文件夹结构? From my understanding, it's poor-form to put /main/main in the location that it currently is. 根据我的理解,将/ main / main放在当前位置的格式不正确。 Do you have recommendations on a new folder structure? 您对新的文件夹结构有建议吗?

A good structure in my opinion: 我认为一个好的结构:

server/
  urls.py
  manage.py
  wsgi.py
  settings.py
  apps/
    app1/
    app2/
    app3/

../main/main/urls.py is your entry point for defining your URL patterns and you have no pattern defined to handle / which is what localhost:8000 would be in this case. ../main/main/urls.py是用于定义URL模式的入口点,并且没有定义用于处理/模式,在这种情况下,它将是localhost:8000 You have two basic options. 您有两个基本选择。

First, if you want the stuff in ../main/app to have a URL that includes /app/ you need to define the URL patterns that will exist at the root level. 首先,如果您希望../main/app中的内容具有包含/app/的URL,则需要定义将在根级别存在的URL模式。 Start by creating a view function for the root URL and add a URL pattern for / , see below: 首先为根URL创建一个视图函数,并为/添加URL模式,如下所示:

../main/main/views.py ../main/main/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("This is the home page/site root")

../main/main/urls.py ../main/main/urls.py

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

urlpatterns = [
    url(r'^$', ), views.index, name='home',
    url(r'^admin/', admin.site.urls),
    url(r'^app/', include('app.urls')),
]

Second option, if you want the views in ../main/app/ to exist under the root URL ( / instead of /app/ ) then just change the base of the imported app URL patterns in ../main/main/urls.py : 第二种选择,如果您希望../main/app/的视图存在于根URL( /而不是/app/ )下,则只需更改../main/main/urls.py导入的app URL模式的../main/main/urls.py

../main/main/urls.py ../main/main/urls.py

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

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

There is nothing wrong with your folder structure. 您的文件夹结构没有任何问题。 There are certainly other layouts you could use for your project, but what you are using is Django's default out of the box layout. 当然,您可以在项目中使用其他布局,但是您使用的是Django默认的开箱即用布局。 Unless you have a specific reason to use a different layout, stick with what you have. 除非有特殊原因要使用其他布局,否则请坚持使用现有布局。 Spend your time learning more about Django, worry about trying to optimize your project layout when you have a better understanding of how Django's pieces all fit together. 花时间学习有关Django的更多信息,当您对Django的各个部分如何组合在一起有更好的了解时,不必担心尝试优化项目布局。

I think what you need to consider is what you want your site's URLS to look like, then build the layout around that. 我认为您需要考虑的是您希望网站的URL看起来像什么,然后围绕该URL构建布局。 You don't need the app app unless you want to organize your code that way. 除非您想要以这种方式组织代码,否则不需要该app程序。 As a starting point you can just put everything you have in the app app under main and expand out to other apps as it makes sense for the project. 首先,您可以将app程序app程序中的所有内容都放置在main下,并根据项目的需要扩展到其他应用程序。

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

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