0

Hi bit of a beginner question about using django's modelchoicefield in a form I'm building.

I just need get django to display a drop down list of ingredients in a form. I've gotten to the point where the page renders but the form does not, I was getting errors before so I am kind of perplexed at the moment. I was hoping for some guidance.

Using python 2.7.6 and django 1.6.2. If I left anything out let me know.

Thanks!

Code is below:

views:

    args = {}
#add csrf sercurity
args.update(csrf(request))

args['form'] = form
return render_to_response('newMeal.html', args)

form:

from django import forms from models import meals, ingredients, recipe

class mealForm(forms.ModelForm):

breakfast = forms.ModelChoiceField(queryset=recipe.objects.all())
# Lunch = forms.ModelChoiceField(queryset=recipe.objects.all())
# Dinner = forms.ModelChoiceField(queryset=recipe.objects.all())


class Meta:
    model = meals
    fields = ('Breakfast','Lunch','Dinner','servingDate')

class recipeForm(forms.ModelForm):

class Meta:
    model = recipe
    fields = ('Name', 'Directions')

template:

    {% extends "base.html" %}

{% block content %}

<p>New Meals go here!</p>

<form action="/meals/newmeal/" method="post">{% csrf_token %}
    <table class="selection">
        {{form.as_table}}


        <tr><td colspan="2"><input type="submit" name="submit" value="Add Meal"></td></tr>
    </table>
</form>

{% endblock %}

Model;

from django.db import models import datetime

Create your models here.

class recipe(models.Model):

Name = models.CharField(max_length=200)
Directions = models.TextField()
pub_date = models.DateTimeField(auto_now_add = True)

def __unicode__(self):
    return (self.id, self.Name)

class ingredients(models.Model):

Name = models.CharField(max_length=200)
Quantity = models.IntegerField(default=0)
Units = models.CharField(max_length=10)
Recipe = models.ForeignKey(recipe)

def __unicode__(self):
    return self.Name

class meals(models.Model):

Breakfast = models.CharField(max_length=200)
Lunch = models.CharField(max_length=200)
Dinner = models.CharField(max_length=200)
servingDate = models.DateTimeField('date published')
5
  • What errors were you getting? Mar 22, 2014 at 13:14
  • I am getting error 200 Mar 22, 2014 at 17:05
  • In that case I'm not getting an error. Which is weird, when I check the rendered page source, the form is there but just the start and ends tags, no content. Figured I was just misusing the ModelChoiceField. Mar 22, 2014 at 19:21
  • Paste your models.py, because you have some basic issues but I want to make sure there isn't anything wrong with your models :) Mar 22, 2014 at 19:57
  • Added the models to my original post. Hope that helps. Mar 22, 2014 at 20:06

1 Answer 1

0

did you import the mealForm:

some thing like :from app.forms import mealForm

form is a function. so try:

args['form'] = mealForm()

Note: don't use render_to_response. it is old use render instead(so don't even need csrf)::

from django.shortcuts import render
def...(request):
....
return render(request,'newMeal.html', {'form': mealForm()})
1
  • thanks for the pro-tip re render_to_response. I double checked and I am importing the form and I get the same thing, pages renders but with no content. If I remove the ModelChoiceField from the form everything renders just dandy. I added the whole form.py code to my original post, I'm sure it's some weird rookie mistake I'm making. Mar 22, 2014 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.