数据未从 Django 表单进入数据库

问题描述 投票:0回答:1

我按“提交”按钮,终端中有“发布”请求,但数据库中没有新模型。

表格.py

class PinCreationForm(forms.ModelForm):
    image = forms.ImageField(widget=forms.ClearableFileInput(attrs={
        'class':'create-pin-file-input'
    }))
    name = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'create-pin-text-input', 'placeholder': 'Богатый мужчина'
    }))
    description = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'create-pin-text-input', 'placeholder': 'Мужик стоит дрочит'
    }))
    tags = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'create-pin-text-input', 'placeholder': 'Спорт, Машина, Огород'
    }))
    class Meta:
        model = PinModel
        fields = ('image', 'name', 'description', 'tags')

模型.py

class PinModel(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField(null=True, blank=True)
    tags = models.TextField()
    image = models.ImageField(upload_to='pin_images')
    user = models.ForeignKey(to=User, on_delete=models.CASCADE)

views.py

class CreatePinView(CreateView):
    model = PinModel
    form_class = PinCreationForm
    template_name = 'pinapp/create-pin.html'
    success_url = reverse_lazy('users:login')

html

section class="create-pin-section">
    <div class="create-pin-div">
        <h2>Create Pin</h2>
        <form action="{% url 'create-pin' %}" class="create-pin-form" method="post">
            {% csrf_token %}
            <label for="{{ form.image.id_for_label }}">Choose Pic</label>
            {{ form.image }}
            <label for="{{ form.name.id_for_label }}">Choose da name</label>
            {{ form.name }}
            <label for="{{ form.description.id_for_label }}">Choose da description</label>
            {{ form.description }}
            <label for="{{ form.tags.id_for_label }}">Choose da tags</label>
            {{ form.tags }}
            <button type="submit">Create</button>
        </form>
    </div>
</section>

我想解释一下,我想要的是“用户”字段填写提交表单的用户

我尝试了我发现的一切,但没有结果。

django django-models django-forms django-templates django-database
1个回答
0
投票

将此添加到您的

CreatePinView
课程中。原因是你没有保存你的记录。

def form_valid(self, form):
        return super().form_valid(form)
© www.soinside.com 2019 - 2024. All rights reserved.