Taziamoma Abraham
Taziamoma Abraham

Reputation: 67

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>

Upvotes: 0

Views: 130

Answers (1)

markwalker_
markwalker_

Reputation: 12849

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

Upvotes: 2

Related Questions