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



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

在我的虚拟环境中, 蟒蛇 3.7.4, 姜戈 2.2.7 我已经安装了Django-widgets-betters-betters。

//视图

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'),
]

初始形式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})

相关内容

  • 没有找到相关文章

最新更新