Django在基于类的视图中处理表单数据

时间:2017-06-27 20:13:13

标签: python django

我正在尝试将一些表单数据保存到django模型,现在当我提交表单页面只是挂起而没有任何反应时,也没有任何内容保存到ItemList模型中。我真的很困惑,不明白我做错了什么。任何帮助是极大的赞赏。

forms.py

class ItemListCreateForm(forms.ModelForm):

    items = forms.ModelChoiceField(
        queryset=item.objects.all(),
        widget=autocomplete.ModelSelect2Multiple()
    )

    class Meta:
        model= ItemList
        # fields = ('__all__')
        fields = ['name', 'description', 'notes', 'items', 'public']

views.py

class ItemListCreateView(FormView, autocomplete.Select2QuerySetView):

    template_name = 'itemlist.html'
    Model = ItemList
    #success_url = 'itemlists'
    form_class = ItemListCreateForm


    def get_queryset(self):
        qs = Item.objects.values_list('symbol')

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs

    def form_valid(self, form):
        form.instance.created_by = self.request.user
        return super(ItemListCreateView, self).form_valid(form)

Models.py

class ItemList(models.Model):
    """
    A list of items that can be selected for use in visualization
    """
    name = models.CharField(max_length = 256)
    created_by  = models.ForeignKey(User, on_delete=models.CASCADE)
    description = models.CharField(max_length = 512)
    notes = models.TextField()
    items = models.ManyToManyField(Gene)
    public = models.BooleanField(default = False)

    def get_absolute_url(self):
        return reverse('itemlistdetail', kwargs={'pk': self.pk})

itemlist.html

{%  extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}

{% block scripts %}
{% endblock %}

{% block styles %}
{% endblock %}

{#{% block title %}Login | {{ block.super }}{% endblock %}#}


{% block content %}
{{ form.media }}

{% block headline %}<h2 align="center">Create Item List</h2>{% endblock %}<br><br>
<div>
<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit">
</form>
</div>
{% endblock %}

0 个答案:

没有答案