服务器错误(500)和表单内的奇怪符号

问题描述 投票:0回答:1

我设置了一个使用 Ubuntu 服务器作为操作系统的 Raspberry Pi。我遵循了这个指南:digitalocean 的指南

一切似乎都很顺利,直到我打开一个页面,其中的表单看起来有些奇怪(“名称:”前后有不需要的括号: Screenshot of the form

背后的代码如下所示:

# models.py
class Category(models.Model):
   name = models.CharField(max_length=50)

   def __str__(self) -> str:
      return self.name

# forms.py
class CategoryForm(forms.ModelForm):
   class Meta:
      fields="__all__"
      model = Category

      widgets = {
         'name': forms.TextInput(attrs={'class': 'form-control'})
      }

# views.py
def categories(request: HttpRequest):
   if request.method == 'POST':
      filledForm = CategoryForm(data=request.POST)
      if filledForm.is_valid():
         newCategory = filledForm
         newCategory.save()

   categoryForm = CategoryForm()
   categories = Category.objects.order_by('name')
   args = {
     'categoryForm': categoryForm,
     'categories': categories,
   }
   return render(request, 'categories.html', args)

和 HTML 代码:

<div class="container">
    <h2>Categories</h2>
    
    <br>
    
    <form action="{% url 'categories' %}" method="post">
        {% csrf_token %}
        {{ categoryForm }}
        <input type="submit" class="form-control btn-secondary" name="submit-new-category" value="Add">
    </form>
    
    <br>
    
    <table class="table table-hover">
        <tbody>
            {% for category in categories %}
            <tr>
                <td>
                    <a href="{% url 'category_edit' category.id %}">
                        <div>
                            {{ category.name }}
                        </div>
                    </a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

当我尝试发布已填写的类别表单时,Apache 会返回服务器错误 (500)。

我检查了日志文件(/var/log/apache2/error.log),但除了警告和通知之外什么也没有:

[Sun Aug 11 13:40:06.992377 2024] [mpm_event:notice] [pid 2809:tid 281473660837920] AH00489: Apache/2.4.58 (Ubuntu) configured -- resuming normal operations
[Sun Aug 11 13:40:06.993125 2024] [core:notice] [pid 2809:tid 281473660837920] AH00094: Command line: '/usr/sbin/apache2'
[Sun Aug 11 13:40:54.766803 2024] [mpm_event:notice] [pid 2809:tid 281473660837920] AH00492: caught SIGWINCH, shutting down gracefully
[Sun Aug 11 13:41:05.325303 2024] [mpm_event:notice] [pid 3058:tid 281473704566816] AH00489: Apache/2.4.58 (Ubuntu) mod_wsgi/5.0.0 Python/3.12 configured -- resuming normal operations
[Sun Aug 11 13:41:05.326039 2024] [core:notice] [pid 3058:tid 281473704566816] AH00094: Command line: '/usr/sbin/apache2'
[Sun Aug 11 14:28:47.835081 2024] [mpm_event:notice] [pid 3058:tid 281473704566816] AH00492: caught SIGWINCH, shutting down gracefully
[Sun Aug 11 14:28:48.320266 2024] [mpm_event:notice] [pid 3971:tid 281473052844064] AH00489: Apache/2.4.58 (Ubuntu) mod_wsgi/5.0.0 Python/3.12 configured -- resuming normal operations
[Sun Aug 11 14:28:48.321026 2024] [core:notice] [pid 3971:tid 281473052844064] AH00094: Command line: '/usr/sbin/apache2'
[Sun Aug 11 14:30:37.648045 2024] [mpm_event:notice] [pid 3971:tid 281473052844064] AH00492: caught SIGWINCH, shutting down gracefully
[Sun Aug 11 14:33:43.893787 2024] [mpm_event:notice] [pid 912:tid 281472884133920] AH00489: Apache/2.4.58 (Ubuntu) mod_wsgi/5.0.0 Python/3.12 configured -- resuming normal operations
[Sun Aug 11 14:33:43.906803 2024] [core:notice] [pid 912:tid 281472884133920] AH00094: Command line: '/usr/sbin/apache2'
[Sun Aug 11 14:41:50.770637 2024] [mpm_event:notice] [pid 912:tid 281472884133920] AH00492: caught SIGWINCH, shutting down gracefully
[Sun Aug 11 14:41:54.169218 2024] [core:warn] [pid 912:tid 281472884133920] AH00045: child process 915 still did not exit, sending a SIGTERM
[Sun Aug 11 14:45:14.149261 2024] [mpm_event:notice] [pid 923:tid 281472851755040] AH00489: Apache/2.4.58 (Ubuntu) mod_wsgi/5.0.0 Python/3.12 configured -- resuming normal operations
[Sun Aug 11 14:45:14.160107 2024] [core:notice] [pid 923:tid 281472851755040] AH00094: Command line: '/usr/sbin/apache2'

在我的开发计算机上使用

python manage.py runserver
运行它一切正常。

现在,我有两个问题:为什么这些括号出现在我的类别表单中以及为什么我收到服务器错误?我在同一个帖子中发布了两个问题,因为我不知道它们是否相关。

python django apache2 ubuntu-server
1个回答
0
投票
  1. 类别表格中出现的括号

    {% csrf_token %} {{categoryForm.name.label_tag }} {{categoryForm.name}}
  1. 提交表单时出现服务器错误(500)

    记录 = { “版本”:1, “disable_existing_loggers”:错误, '处理程序':{ '文件': { '级别':'调试', 'class': 'logging.FileHandler', '文件名': '/path/to/django/debug.log', }, }, '记录器':{ 'django': { '处理程序':['文件'], '级别':'调试', “传播”:确实, }, }, }

检查代码中的潜在问题

if filledForm.is_valid():
newCategory = filledForm.save(commit=False)
newCategory.save()
© www.soinside.com 2019 - 2024. All rights reserved.