Django表单在模板页面上未显示错误

时间:2019-11-25 06:13:13

标签: django django-forms django-templates django-widget-tweaks

我的代码与此https://simpleisbetterthancomplex.com/series/2017/09/18/a-complete-beginners-guide-to-django-part-3.html#rendering-bootstrap-forms完全相同。但是,当我单击模板中的“发布”按钮时,它会向我显示同一页面,而不会出现类似该字段的错误。

在我的virtualenv中, Python 3.7.4, 的Django 2.2.7 并且我已经安装了Django-widgets-improved。

//视图

def new_topic(request, pk):
    board = get_object_or_404(Board, pk=pk)
    user = User.objects.first()

    if request.method == 'POST':
        form = NewTopicForm(request.POST)
        if form.is_valid():
            topic = form.save(commit=False)
            topic.board = board
            topic.starter = user
            topic.save()

            post = Post.objects.create(
                message = form.cleaned_data.get('message'),
                topic = topic,
                created_by = user
            )
            return redirect('board_topics', pk = board.pk)

    form = NewTopicForm()
    return render(request, 'new_topic.html', {'board': board, 'form': form})

//表单html

   <form method="post" novalidate>
            {% csrf_token %}
            {{ form.non_field_errors }}
            {{ form.errors }}
            {{ form.as_p }}

            <button type="submit" class="btn btn-success">Post</button>
        </form>

// models.py

from django.db import models
from django.contrib.auth.models import User


class Board(models.Model):
    name = models.CharField(max_length=25, unique=True)
    description = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class Topic(models.Model):
    subject = models.CharField(max_length=255)
    last_updated = models.DateTimeField(auto_now_add=True)
    board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='topics')
    starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')


class Post(models.Model):
    message = models.TextField(max_length=4000)
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name='posts')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    updated_by = models.ForeignKey(User, null=True, on_delete=models.CASCADE, related_name='+')

// forms.py

from django import forms
from .models import Topic


class NewTopicForm(forms.ModelForm):
    message = forms.CharField(
        widget=forms.Textarea(
            attrs={'rows':5, 'placeholder':'What is in your mind?'}
        ), 
        max_length=4000,
        help_text='The max length of the text is 4000.')

    class Meta:
        model = Topic
        fields = ['subject', 'message']

// main / urls.py

from django.contrib import admin
from django.urls import path
from boards import views


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('boards/<int:pk>/', views.board_topics, name='board_topics'),
    path('boards/<int:pk>/new/', views.new_topic, name='new_topic'),
]

1 个答案:

答案 0 :(得分:0)

必须在form = NewTopicForm()之前或在if request.method == 'POST':条件下声明初始格式else:。对于您而言,干净的表单始终会到达模板。

def new_topic(request, pk):
    board = get_object_or_404(Board, pk=pk)
    user = User.objects.first()

    form = NewTopicForm()  # <---

    if request.method == 'POST':
        form = NewTopicForm(request.POST)
        if form.is_valid():
            topic = form.save(commit=False)
            topic.board = board
            topic.starter = user
            topic.save()

            post = Post.objects.create(
                message = form.cleaned_data.get('message'),
                topic = topic,
                created_by = user
            )
            return redirect('board_topics', pk = board.pk)

    return render(request, 'new_topic.html', {'board': board, 'form': form})

def new_topic(request, pk):
    board = get_object_or_404(Board, pk=pk)
    user = User.objects.first()

    if request.method == 'POST':
        form = NewTopicForm(request.POST)
        if form.is_valid():
            topic = form.save(commit=False)
            topic.board = board
            topic.starter = user
            topic.save()

            post = Post.objects.create(
                message = form.cleaned_data.get('message'),
                topic = topic,
                created_by = user
            )
            return redirect('board_topics', pk = board.pk)
    else:
        form = NewTopicForm()  # <---

    return render(request, 'new_topic.html', {'board': board, 'form': form})