Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

python - How to change django's validation error in html

When I submit a form and want the required error to appear when I submit a blank field, I get another error message, specifically "answer", how do I delete it? html is like this -> <ul class =" errorlist "> <li> answer <ul class =" errorlist "> <li> This field is required. </li> </ul> </li> </ ul>. image

I found the options in google but it did not work

i want only "This field is required."

Thanks in advance

models.py

from django.db import models
from django.core.exceptions import ValidationError

class Question(models.Model):
    question=models.CharField(max_length=100)
    answer_question=models.CharField(max_length=100, default=None)

    def __str__(self):
        return self.question

    
class Answer(models.Model):
    questin=models.ForeignKey(Question, on_delete=models.CASCADE, related_name="questions", blank=True)
    answer=models.CharField(max_length=100, null=True)

    
    def __str__(self):
        return str(self.questin)

views.py

from django.shortcuts import render
from django.shortcuts import render, HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from .forms import QuestionForm,AnswerForm
from .models import Question,Answer
import random
from django.forms import modelformset_factory



def home(request):
    form=QuestionForm
    if request.method=='POST':
        form=QuestionForm(request.POST)
        if form.is_valid():
            form.save()

    return render(request, "question/base.html", {"form":form})

def ans(request):
    questions=Question.objects.all()
    form=AnswerForm()
    if request.method=="POST":
        form=AnswerForm(request.POST)
        if form.is_valid():
            ques=request.POST.getlist("question_id")
            ans=request.POST.getlist("answer")
            for i in range(len(ques)):
                form=Answer(questin=Question.objects.get(id=ques[i]), answer=ans[i])
                form.save()
            return redirect(request.path)
    return render(request, "question/ans.html", {"form":form, "questions":questions})

ans.html

<!DOCTYPE html>
<html>
    <head>
        <title>question</title>
    </head>

    <body>
        
        
        <form method="POST" novalidate>
            {% csrf_token %}
            {% for question in questions %}
            <input type="hidden" name="question_id" value="{{ question.id }}"/>

                {{question}}
                {{form.answer}}
                {{form.errors}}
            {% endfor %}
                
                <input type="submit" name="sub">
                
        </form>
        
    </body>
</html>
question from:https://stackoverflow.com/questions/66067260/how-to-change-djangos-validation-error-in-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...