简体   繁体   中英

Django form not showing except for submit button

I'm having this issue where the django form isn't showing up on the webpage. The only thing that shows up is the submit button. I cannot figure out the issue.

Views.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'}),
        }

newthread.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>

Your context isn't returned so there isn't anything for the template to render. You need to return the context;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM