0

我已经尝试寻找有关如何检查表单名称是否已存在于数据库中的解决方案。我用这个链接来弄清楚如何,它确实不允许输入重复的名字。但是在我期望的地方,我没有收到错误消息。我不确定我在这里做错了什么,所以如果有人能告诉我我应该做什么,那将非常有用!

addgame.html:

<form method="POST" class="post-form" enctype="multipart/form-data">
          {% csrf_token %}
          {% if form.non_field_errors %}
              {% for error in form.non_field_errors %}
                {{ error }}
              {% endfor %}
          {% endif %}
          <div class="form-group">
            {{ form.name.label_tag }}
            {% render_field form.name class="form-control" %}
            <br>
            {{ form.genre.label_tag }}
            {% render_field form.genre class="form-control" %}
            <br>
            {{ form.image.label_tag }}
            {{ form.image }}
          </div>
          <hr>
          <button type="submit" class="save btn btn-primary">Save</button>
        </form>

视图.py:

def addgame(request):
if request.method == "POST":
    form = InfoForm(request.POST, request.FILES)
    if form.is_valid():
        infolist = form.save(commit=False)
        infolist.created_date = timezone.now()
        infolist.save()
        return redirect('index')
else:
    form = InfoForm()
return render(request, 'ranking/addgame.html', {'form': form})

表格.py:

class InfoForm(forms.ModelForm):

class Meta:
    model = GameInfo
    fields = ('name', 'image', 'genre')

def clean_name(self):
    name = self.cleaned_data['name']
    try:
        match = GameInfo.objects.get(name=name)
    except GameInfo.DoesNotExist:
        return name
    raise forms.ValidationError('This game has already been added to the list.')

不确定是否需要,所以我也会发布models.py:

class GameInfo(models.Model):
GAME_CHOICE = [
    ("BMU", "Beat 'em up"),
    ("FT", "Fighting"),
    ("PF", "Platform"),
    ("FPS", "Shooter"),
    ("SV", "Survival"),
    ("ST", "Stealth"),
    ("AA", "Action Adventure"),
    ("EX", "Exploring"),
    ("SH", "Survival horror"),
    ("IF", "Interactive fiction"),
    ("IM", "Interactive movie"),
    ("VN", "Visual novel"),
    ("ARP", "Action role-playing"),
    ("JRP", "Japanese role-playing"),
    ("TRP", "Tactical role-playing"),
    ("CAM", "Construction and management"),
    ("LS", "Life simulation"),
    ("SP", "Sports"),
    ("VH", "Vehicle"),
    ("MOBA", "Multiplayer online battle arena"),
    ("RTS", "Real-time strategy"),
    ("RTT", "Real-time tactics"),
    ("TBS", "Turn-based strategy"),
    ("TBT", "Turn-based tactics"),
    ("MMORPG", "MMORPG"),
    ("MMOFPS", "MMO-FPS"),
    ("MMOR", "MMO Racing"),
    ("CG", "Cardgame"),
    ("PAC", "Point and Click"),
    ("MG", "Music Game"),
    ("VR", "Virtual Reality"),
    ("RC", "Racing"),
]
name = models.CharField(max_length=100)
created_date = models.DateTimeField(default=timezone.now)
image = models.ImageField(upload_to='./media/images/')
genre = models.CharField(
    max_length=6,
    choices=GAME_CHOICE,
    default="BMU",
)

def __str__(self):
    return self.name

class Meta:
    ordering = ('name',)
4

1 回答 1

3

如果您手动呈现表单字段,则由您决定是否包含错误,例如:

{{ form.name.errors }}
{{ form.name.label_tag }}
{% render_field form.name class="form-control" %}
于 2017-12-21T14:52:50.510 回答