简体   繁体   English

Django2未连接到本地主机

[英]Django2 not connecting to Local Host

I'm new to Django/Python. 我是Django / Python的新手。 I'm currently taking a beginners course and I'm having difficulty setting up my URLs and connecting to a local server. 我目前正在参加初学者课程,但是在设置URL和连接到本地服务器时遇到困难。 I'm using Python version: 3.7.0 and Django v.2 我正在使用Python版本:3.7.0和Django v.2

The command line does not give any errors (0), it tells me to go here: 命令行没有给出任何错误(0),它告诉我去这里:

Starting development server at http://127.0.0.1:8000/

Which I believe is the local host, however the site says 我相信是本地主机,但是该网站说

This site can’t be reached
127.0.0.1 refused to connect.

Can anyone let me know what I'm missing I would greatly appreciate it so that I can continue on with my studying. 谁能让我知道我想念的东西,对此我将不胜感激,这样我就可以继续学习。 I'm using tutorials on YT Django Tutorial and the guy is using an older Django so I think that may be why I'm having trouble. 我正在使用YT Django教程上的教程 ,而那个家伙正在使用旧的Django,所以我认为这可能就是我遇到麻烦的原因。 He says we should still be okay to follow through even on the new version of Django. 他说,即使在新版本的Django上,我们也应该可以继续进行下去。

urls.py urls.py

from django.contrib import admin
from django.urls import path
from.import views

urlpatterns = [
    path('', views.homepage),
    path('admin/', admin.site.urls),
    path('about/', views.about),

]

views.py views.py

from django.http import HttpResponse

def homepage(request):
    return HttpResponse('homepage')
def about(request):
    return HttpResponse('about')

Got it to work. 得到它的工作。 This is what was needed: 这是需要的:

ALLOWED_HOSTS = ['localhost', '127.0.0.1']

Took me forever to figure this out. 永远让我想通这个。 :) :)

There is a little modification is required in your code: Instead of path('about/', views.about), write this: 您的代码中需要进行一些修改:代替path('about/', views.about),编写以下代码:

path('about/', views.about,name='about'),

after modify this line , run the server and open this http://127.0.0.1:8000/ URL and add /about and then press enter. 修改此行后,运行服务器并打开此http://127.0.0.1:8000/ URL并添加/ about,然后按Enter。

http://127.0.0.1:8000/about http://127.0.0.1:8000/关于

Hope it will work in your case 希望它能在您的情况下起作用

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

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