Apache上的Django重定向不正确

时间:2017-09-08 16:16:25

标签: python django apache mod-wsgi

我目前正在Apache服务器上托管我的Django站点。 Django站点在本地托管时工作正常,但是当我尝试导航到站点的不同部分时我从服务器托管它时会出现错误。按照现在的情况,您可以通过导航到example.com/djproj进入主页,这样就可以拉出登录屏幕。当您输入凭据并点击提交按钮时,它会转到example.com但不支持,但如果您手动导航回example.com/djproj,它会将您带到主屏幕并且您已登录,单击任何按钮会出现类似的错误。值得注意的是,如果您尝试通过直接输入支持的URL(urls文件中的一个),即example.com/djproj/precheck/,则会得到404,说明请求网址为example.com//precheck/

我会尝试提供我认为适用的脚本,但如果我遗漏了一些我很乐意提供的内容。

文件结构类似于/var/www/html/djproj/cif,其中cif包含所有典型的django文件,例如manage.py和相关的应用程序文件夹。

wsgi.py

import os, sys

from django.core.wsgi import get_wsgi_application

path = 'var/www/djproj/grip/cif'

if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "grip.settings")

application = get_wsgi_application()

urls.py

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/', include('accounts.urls')),
    url(r'^precheck/', include('precheck.urls')),
    url(r'^[/]?$', views.upload, name = 'home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

httpd.conf (适用部分)

WSGIScriptAlias /djproj/ /var/www/html/djproj/cif/grip/wsgi.py process-group=16$
WSGIPythonPath /var/www/html/djproj/

<Directory /var/www/html/djproj/cif/grip>
   <Files wsgi.py>
      Order deny,allow
      Allow from all
   </Files>
</Directory>

编辑:在评论中尝试建议。

我将网址更改为以下内容:

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^djproj/accounts/', include('accounts.urls')),
    url(r'^djproj/*precheck/', include('precheck.urls')),
    url(r'^djproj[/]?$', views.upload, name = 'home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

当我尝试导航到example.com/djproj它说Page not found at /时,似乎当Apache传回url时它正在丢弃example.com/djproj。同样,如果我尝试example.com/djproj/precheck/,我会得到一个404,其请求网址为example.com//precheck/,并且显示The current path, /precheck/, didn't match any of these.'这些'是上面urlpatter中列出的网址。

编辑(显示网址的编码方式):

upload.html

<form method = "POST" action="{% url 'precheck:upload' %}" enctype="multipart/form-data" name="uploadForm">
    {% csrf_token %}

    {% for field in form %}
    <div class="form-row">
        {{field.errors }}
        {{ field.label_tag }} {{ field }}
        {% if field.help_text %}
            <p class ="help-text">{{ field.help_text }}</p>
        {% endif %}
    </div>
    {% endfor %}

    <br />
    <button type="button" id="uploadButton" data-loading-text="Loading..." class="btn btn-primary" autocomplete="off">Upload</button>

</form>

请注意使用{% url 'precheck:upload'}以下是prechecks.urls中指向的位置:

urls.py - 预先检查

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

app_name = 'precheck'

urlpatterns = [
    url(r'^success/', views.successful_upload, name = 'successful_upload'),
    url(r'^failed_upload/', views.failed_upload, name = 'failed_upload'),
    url(r'^run_precheck/', views.run_precheck, name = 'run_precheck'),
    url(r'^running_checks/', views.run_selected_checks, name = 'run_selected_checks'),
    url(r'^checks_successful/', views.checks_successful, name = 'checks_successful'),
    url(r'^checks_failed/', views.checks_failed, name = 'checks_failed'),
    url(r'^$', views.upload, name = 'upload'),
]

views.py

@login_required
def upload(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES, user = request.user)
        if form.is_valid():
            form.save()
            request.session['jobID'] = request.POST['jobID']
            #job = Job.objects.filter(user_id = request.user.id).filter(jobID = request.POST['jobID']).latest()
            # initialize(job)
            return render(request,'precheck/run_precheck.html')
    else:
        form = UploadFileForm()
    return render(request, 'precheck/upload.html',{'form': form})

1 个答案:

答案 0 :(得分:0)

您好,可以试试这篇文章

django apache configuration

也参考原始文件

Django with Apache and mod_wsgi