简体   繁体   English

尝试访问网站的域名时为什么会出现404错误,但是如果我的域名/住所能正常工作?

[英]Why am I getting a 404 error when trying to access my website's domain name, yet if I domain name/home it works?

Okay... let's try to explain things clearly. 好吧...让我们尝试清楚地解释一下。 I've used Python Django to create a dynamic webpage/web-app. 我使用Python Django创建了动态网页/网络应用。 After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. 完成网站后,我已使用DigitalOcean发布了该网站,并将我购买的域名成功附加到DigitalOcean的名称服务器上。 When I access my website, ordinanceservices.com, i get an error 404; 当我访问我的网站ordinanceservices.com时,出现错误404。 however, if I type ordinanceservices.com/home it works as it should and displays the home page. 但是,如果我键入ordinanceservices.com/home,它将正常运行并显示主页。 How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? 如何通过编辑python文件,将其保存到ordinanceservices.com将显示主页而不是错误404的位置? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary. 我觉得我正在执行的某些操作从根本上来说对我的.urls结构是错误的,因此不需要在nginx配置中进行重写/重定向。

Here is the specific error: 这是具体的错误:

Page not found (404) 找不到页面(404)

Request Method: GET 请求方法:GET

Request URL: http://ordinanceservices.com/ 要求网址: http : //ordinanceservices.com/

Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order: Django使用django_project.urls中定义的URLconf,按以下顺序尝试了以下URL模式:

^admin/ ^管理员/

^ ^home/ [name='home'] ^ ^ home / [name ='home']

^ ^contact/ [name='contact'] ^ ^ contact / [name ='contact']

^ ^services/ [name='services'] ^ ^ services / [name ='services']

The current URL, , didn't match any of these. 当前网址与此不匹配。

I somewhat understand what is happening here though I do not know how to fix this. 我有点了解这里发生了什么,尽管我不知道该如何解决。 Next I will provide my .urls files for each folder that contains such: 接下来,我将为每个包含以下内容的文件夹提供.urls文件:

/django_project urls.py / django_project urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',


    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('company.urls')),
)

/company urls.py /公司urls.py

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

urlpatterns = [
    url(r'^home/', views.home, name='home'),
    url(r'^contact/', views.contact, name='contact'),
    url(r'^services/', views.services, name='services'),

]

/company views.py / company views.py

from django.shortcuts import render

def home(request):
    return render(request, 'company/home.html')

def contact(request):
    return render(request, 'company/contact.html')

def services(request):
    return render(request, 'company/services.html')

What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; 我的目标是编辑我的Python文件的url和视图结构,以确保常规URL ordinanceservices.com实际上将显示一个URL,而无需使用nginx配置文件来重定向主URL。页; preferably the home page of my webpage. 最好是我网页的首页。

I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. 我有一个预感,这与我没有r'^ admin /'可以到达的views.index有关。 I am not certain but I have been trying to figure this out for hours. 我不确定,但已经尝试了好几个小时了。 Does anyone have a clue what I can do to fix this? 有人知道我能做什么来解决这个问题吗?

You haven't defined anything at the root url. 您尚未在根URL上定义任何内容。 Add one more line to your company urls.py so it becomes 在公司的urls.py中再添加一行,使其成为

//company urls.py
from django.conf.urls import url, include
from . import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^home/', views.home, name='home'),
    url(r'^contact/', views.contact, name='contact'),
    url(r'^services/', views.services, name='services'),

]

First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py 首先,检查您是否在settings.py中将www.yourdomain.comyourdomain.com添加到ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com']

and then in your company/urls.py do this 然后在您的公司/urls.py中执行此操作

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

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^services/$', views.services, name='services'),

]

and in your main urls.py add this code 并在您的主要urls.py中添加此代码

from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()

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

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

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