簡體   English   中英

Django 表單不顯示,除了提交按鈕

[英]Django form not showing except for submit button

我遇到了 django 表單未顯示在網頁上的問題。 唯一顯示的是提交按鈕。 我無法弄清楚這個問題。

視圖.py

class NewThreadView(CreateView):
    model = Thread
    form_class = NewThreadForm
    template_name = 'thread/newthread.html'

    def get_context_data(self, *args, **kwargs):
        context = super(NewThreadView, self).get_context_data(*args, **kwargs)

forms.py

class NewThreadForm(forms.ModelForm):
    class Meta:
        model = Thread
        fields = ('name', 'body', 'author', 'thread_forum')

        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter title'}),
            'body': forms.Textarea(attrs={'class': 'form-control'}),
            'author': forms.TextInput(attrs={'class': 'form-control', 'value': '', 'id': 'author', 'type': 'hidden'}),
            'thread_forum': forms.Select(attrs={'class': 'form-control', 'type': 'hidden'}),
        }

新線程.html

<div class="form-group">
     <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn btn-primary" name="thread_submit">Create Thread</button>
    </form>
</div>

您的上下文未返回,因此模板無法呈現任何內容。 您需要返回上下文;

class NewThreadView(CreateView):
    model = Thread
    form_class = NewThreadForm
    template_name = 'thread/newthread.html'

    def get_context_data(self, *args, **kwargs):
        context = super(NewThreadView, self).get_context_data(*args, **kwargs)
        return context

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM