how to populate dropdown list in Django from a for

2019-08-03 06:23发布

I have two models.

class ArticleCategory(models.Model):
    category = models.CharField(max_length=255,blank=False)

class Article(models.Model):
    title = models.CharField(max_length=255,blank=False)
    body = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey(ArticleCategory,default=1)

Now I have to render a template and save the form for Article model. I have a foreignKey field in my Article Model and because of that I'm not able to save my article form. I want to select a category from dropdown list and save it in my Article model.

How should I code my template for this ?

My views.py function for this is:

def create(request):
    if request.POST:
        form = ArticleForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/articles/all/')
    else:
        form = ArticleForm()
        args={}
        args.update(csrf(request))
        args['categories'] = ArticleCategory.objects.all()
        args['form'] = form
        return render_to_response('create_article.html', args)

My template create_article.html currently looks like this:

<form role="form" action="/articles/create/" method="POST" enctype="multipart/form-data">{% csrf_token %}
                        <div class="row">
                            <div class="form-group col-lg-3">
                            <label></label>
                                <p>{{form.title}}</p>
                            </div>
                            <div class="form-group col-lg-3">
                                <label>Category</label>
                                <p>
                                    <select id="id_category">
                                        {% for category in categories  %}
                                        <option value="{{ category }}">{{ category.category }}</option>
                                        {% endfor %}
                                    </select>
                                </p>
                            </div>
                            <div class="clearfix"></div>
                            <div class="form-group col-lg-12">
                                <label>Body</label>
                                {{form.body}}
                            </div>
                            <div class="form-group col-lg-12">
                                <button type="submit" class="btn btn-default">Save Article</button>
                            </div>
                        </div>
                    </form>

2条回答
Emotional °昔
2楼-- · 2019-08-03 07:04

You don't need to do this manually. If your ArticleForm is ModelForm and doesn't exclude category field then you can just write {{ form.category }} and get dropdown created by django automatically. It uses ModelChoiceField underneath the hood.

查看更多
beautiful°
3楼-- · 2019-08-03 07:25

replace

<select id="id_category">
  {% for category in categories  %}
    <option value="{{ category }}">{{ category.category }}</option>
  {% endfor %}
</select>

with

{{ form.category }}
查看更多
登录 后发表回答