You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache mod_wsgi部署Django 404与代码不生效问题解决

问题现象

在Ubuntu 20.04.4上搭建由Apache、MariaDB、Python组成的LAMP环境部署Django项目时,Django可正常在数据库中创建数据表,但通过浏览器访问URL http://127.0.0.1/vcm/create_unit 加载表单时返回404错误;使用Python自带开发服务器访问http://127.0.0.1:8000/vcm/create_unit可正常运行。

解决方法

执行以下命令重启Apache服务即可恢复正常:

sudo systemctl restart apache
问题原因

Apache通过mod_wsgi运行Python项目时不会自动重载修改后的Python文件,代码更新后无法即时生效,重启Apache即可加载最新版本的代码。Python开发服务器自带代码热重载机制,不存在该问题,开发阶段推荐使用Python自带开发服务器调试,减少不必要的服务重启操作。


可正常运行的配置与代码参考

Apache apache2.conf 配置

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

WSGIScriptAlias / /opt/vcm_project/vcm_project/wsgi.py
WSGIPythonHome /opt/env
WSGIPythonPath /opt/vcm_project

<Directory /opt/vcm_project/vcm_project>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

settings.py 模板路径配置

TEMPLATES配置项内硬编码的.html文件路径配置:

'DIRS': [ '/opt/vcm_project/vcm/templates/vcm' ],

应用urls.py 路由代码

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('create_unit', views.create_unit, name='create_unit'),
]

models.py 数据模型代码

from django.db import models

# Create your models here.
class Unit(models.Model):
    unit_code = models.CharField(max_length=15)
    unit_name = models.CharField(max_length=255)
    unit_sector = models.CharField(max_length=255)
    unit_application = models.TextField(max_length=65535)
    unit_url = models.URLField(max_length=255)
    superseded_equivalent = models.CharField(max_length=15)

    def __str__(self):
        return self.unit_name

class Element(models.Model):
    element_number = models.CharField(max_length=100)
    element_name = models.CharField(max_length=255)

    def __str__(self):
        return self.element_name

forms.py 表单代码

from django.forms import ModelForm
from .models import Unit

class UnitForm(ModelForm):
    class Meta:
        model = Unit
        fields = ('__all__')

views.py 视图代码

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from .models import Unit
from django.views.generic.edit import CreateView, UpdateView

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


def create_unit(request):
    if request.method == 'POST':
        # 实例化表单并填充请求提交的数据
        form = UnitForm(request.POST)
        # 校验表单有效性
        if form.is_valid():
            # 处理form.cleaned_data中的合法数据
            form.save()
            # 重定向到目标URL
            return HttpResponseRedirect('/thanks/')

    # GET或其他请求方式时返回空白表单
    else:
        form = UnitForm()

    return render(request, 'vcm/create_unit.html', {'form': form})

HTML模板配置

模板文件存放路径:/opt/vcm_project/vcm/templates/vcm/create_unit.html

create_unit.html 代码:

<form action="{% url 'create_unit' %}" method="POST">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit"/>
</form>

admin.py 后台注册代码

from django.contrib import admin

# 注册模型到后台管理站点
from .models import Unit
admin.site.register(Unit)

from .models import Element
admin.site.register(Element)

内容的提问来源于stack exchange,提问作者Harley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 03:33:33