django ValueError:尝试使用脆脆形式时

anauzrmj  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(124)

我尝试使用crispy表单来显示插入数据的表单。我有一个模型:

class Athlete(models.Model):
    athlete_name=models.CharField(max_length=50)
    GENDER_CHOICES=(
        ('M','Male'),
        ('F','Female'),
        ('O','Others')
    )
    gender=models.CharField(choices=GENDER_CHOICES,max_length=100)
    age=models.IntegerField()
    athlete_category=models.ForeignKey(Category,on_delete=models.CASCADE)
    image=models.FileField(upload_to='static/athlete_img', null=True)
    COUNTRY_CHOICES=(
        ('np','nepal'),
        ('in','india'),
        ('uk','united kingdom'),
        ('sp','spain'),
        ('ch','china')
    )
    medals=models.IntegerField
    country=models.CharField(choices=COUNTRY_CHOICES,max_length=100)
    def __str__(self):
        return self.athlete_name

在forms.py中,我将modelform设置为:

class AthleteForm(ModelForm):
    class Meta:
        model:Athlete
    fields='__all__'

在我的www.example.com中views.py我有如下功能:

def add_athlete(request):
    if request.method == 'POST':
        form = AthleteForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS,
                                 'Athlete added sucessfully')
            return redirect('/admin/athletes')
        else:
            messages.add_message(request, messages.ERROR,
                                 'Enter the appropriate values')
            return render(request, 'forgame/addathletes.html', {
                'form': form
            })
    context = {
        'form': AthleteForm
    }
    return render(request, 'forgame/addathletes.html', context)

在我的templates/for game中,我创建了addathletes.html

{% extends 'layouts.html' %}
{% load crispy_forms_tags %}
{% block title %}
<title>Game Category</title>
{%endblock%}

{% block main_content %}
<div class="container-fluid mt-4">
    <div class="d-flex justify-content-center">
        <div class="col-md-6">
            <h2>Add Categories Here!</h2>
            {% for msg in messages %}
            {% if msg.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %}
            <div class="alert alert-success">
                {{msg}}
            </div>
            {%endif%}

            {% if msg.level == DEFAULT_MESSAGE_LEVELS.ERROR %}
            <div class="alert alert-danger">
                {{msg}}
            </div>
            {%endif%}
            {%endfor%}

            <form action="" method="post" class="shadow-lg p-3">
                {%csrf_token%}
                {{form | crispy}}
                <div class="mt-3">
                    <input type="submit" value="Add Category" class="btn btn-primary">
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

我的网址看起来很好,但我一直得到这个错误:

沿着这个:

svgewumm

svgewumm1#

它应该是=而不是:,因此:

class AthleteForm(ModelForm):
    class Meta:
       model = Athlete
       fields='__all__'

我还建议您保持模板标签之间的差距,就像它应该是{% endblock %}而不是{%endblock%}一样,每个标签都是如此。

相关问题