Django form.py 没有更新

Django form.py not updating

我一直在经历 Django forms 'tutorial'。阅读教程后,我尝试修改它以满足我的需要并对其进行自定义以更好地学习 Django 形式。我发现每当我修改表格时,网站都不会更新。我假设它是我的代码错误,但我找不到它。

# views.py
def contact(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = ContactForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/message_recived/')

# forms.py
from django import forms
class ContactForm(forms.Form):
    name = forms.CharField(label='Name', max_length=100)
    email = forms.EmailField(label='Email', max_length=100)
    message = forms.CharField(label='Message', max_length=500)


# models.py
from django.db import models
class Contact(models.Model):
    name = models.CharField(max_length=100)
    email = models.CharField(max_length=100)
    message = models.CharField(max_length=500)

这里是 contact.html 模板:

#contact.html
{% extends "BlogHome/headerAndFooter.html" %}
{% block content %}
<script>
document.title = "Pike Dzurny - Contact"
</script>
<form action="/message_recived/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
{% endblock %}

我是不是做错了什么?我试过清除浏览器缓存,使用新浏览器,显然刷新它。

您似乎忘记了 render 在您的视图中进行响应。 您还需要将表单包含到上下文中才能正确呈现模板。

尝试改变视图如下:

def contact(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = ContactForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/message_recived/')
    else:
        form = ContactForm()       
    return render(request, 'contact.html', {'form': form})