1

I'm making a web app for post blogs.And I added categories field to identify the category of that blog.But the problem is I can add categories in only when I on admin page.But I want to have that add category button in fronend as well.
example: when I click categories field it shows up current categories in database but I want to add add button to that drop down menu of category.

.this is picture of my add post page
enter image description here

model.py

class PostForm(forms.ModelForm):
    category = forms.ModelChoiceField(queryset=category.objects.all().order_by('name'))
    class Meta:
        model = Post
        fields = ('title', 'category','author', 'content', 'image','status')

template

    {% if user.is_authenticated %}
        <h1>Add Post...</h1>
        <br/><br/>

        <div class="form-group">
<form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form.media }}
            {{ form|crispy}}
            <br>
            <button class="btn btn-secondary btn-lg">Post</button>
            </form>
  • Check https://stackoverflow.com/questions/18602563/django-modelchoicefield-has-no-plus-button/ – lucutzu33 Aug 24 '21 at 20:25
  • Does this answer your question? [Django ModelChoiceField has no plus button](https://stackoverflow.com/questions/18602563/django-modelchoicefield-has-no-plus-button) – lucutzu33 Aug 24 '21 at 20:25

1 Answers1

0

in your form add this

class PostForm(forms.ModelForm):
    category = forms.ModelChoiceField(queryset=category.objects.all().order_by('name'))
    class Meta:
        model = Post
        fields = ('title', 'category','author', 'content', 'image','status')
    def __init__(self, *args, **kwargs):
    super(PostForm, self).__init__(*args, **kwargs)
    add_related_field_wrapper(self, 'category')

this will work fine for you i guess

Shreyash mishra
  • 738
  • 1
  • 7
  • 30