简体   繁体   English

在子目录或 suburl 中运行 Django 时出现 url 问题

[英]Issues in urls when running Django in subdirectory or say suburl

I am trying to run Django inside WordPress like WordPress at main url www.wptesting.com and Django at suburl www.wptesting.com/django .我正在尝试在 WordPress 内运行 Django,例如 WordPress 在 main url www.wptesting.com和 Django 在 suburl www.wptesting.com/django Django main root url Is working fine at www.wptesting.com/django but its suburl eg, admin is not working as it should be www.wptesting.com/django/admin . Django 主根 url 在www.wptesting.com/django上工作正常,但它的 suburl 例如 admin 不工作,因为它应该是www.wptesting.com/django/admin However, whenever I tried to request admin url it goes converts into www.wptesting.comhttp%3a//wptesting.com/django/admin但是,每当我尝试请求管理员 url 时,它都会转换为www.wptesting.comhttp%3a//wptesting.com/django/admin

I am running WordPress and Django with Apache and mod_wsgi , my conf file for apache is as follows:我正在运行 WordPress 和 Django 与Apachemod_wsgi ,我的 apache 的 conf 文件如下:

<VirtualHost *:80>

WSGIScriptAlias /django /path_to_project/wsgi.py

ServerName wptesting.com
ServerAlias www.wptesting.com

DocumentRoot /var/www/html/wordpress

<Directory /var/www/html/wordpress/>
AllowOverride All
Order allow,deny

allow from all
#            Options Indexes FollowSymLinks
#            Require all granted
</Directory>


<Directory /path_to_project/>
            Options Indexes FollowSymLinks
            Require all granted
</Directory>


</VirtualHost>

I have asked one question earlier about configuring Django from subdirectory of WordPress with Apache and wsgi -> you can see the question here我之前问过一个关于从 WordPress 的子目录中使用 Apache 和 wsgi 配置 Django 的问题 -> 你可以在这里看到这个问题

Also If I tried to access any url which is not in Django project then it is giving the standard 404 not found error but when I try to access any valid url like admin it is giving the error mention above.此外,如果我尝试访问不在 Django 项目中的任何 url,那么它会给出标准的 404 未找到错误,但是当我尝试访问任何有效的 url(如管理员)时,它会给出上述错误。

Edited: My Urls.py file:编辑:我的 Urls.py 文件:

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

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'dev_redis.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^$',TemplateView.as_view(template_name='index.html')),
    url(r'^admin/', include(admin.site.urls)),

    url(r'^cache/', 'redis_app.views.redisTest'),

)

Firstly, Django and apache run as backend webservers.首先,Django 和 apache 作为后端网络服务器运行。 Therefore, you can solve this by running apache and Django on two separate ports.因此,您可以通过在两个单独的端口上运行 apache 和 Django 来解决此问题。

Then you can redirect from the current Django site to the new site using HttpResponseRedirect .然后,您可以使用HttpResponseRedirect从当前 Django 站点重定向到新站点。

That's the old format for urls.py.这是 urls.py 的旧格式。 The current is this:目前是这样的:

"""monero URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from monero import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('/', views.py),
]

This is actually the file that is made when you make a new django project.这实际上是您创建新的 django 项目时生成的文件。

嘿,我在您的网址中遇到问题,请尝试:

www.wptesting.com/admin

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

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