Displaying model in a template.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Displaying model in a template.

Solomon Mbak
I have a code that looks like this:

class Courses(models.Model):
    CourseName = models.CharField(max_length = 250)
    CourseDescription = models.CharField(max_length = 250)



class Questions(models.Model):
    CAT_CHOICES = models.ForeignKey("main.course_category", on_delete=models.CASCADE)
    question = models.CharField(max_length = 250)
    optiona = models.CharField(max_length = 100)
    optionb = models.CharField(max_length = 100)
    optionc = models.CharField(max_length = 100)
    answer = models.CharField(max_length = 100)
    catagory = models.CharField(max_length=20, choices = CAT_CHOICES)


The work well. The issue is with the views.py (I suspect).

def home(request):
    choices = Questions.CAT_CHOICES
    print(choices)
    return render(request,
        'quiz/home.html',
        {'choices':choices})



ERRORS:
quiz.Questions.catagory: (fields.E004) 'choices' must be an iterable (e.g., a list or tuple).


What I want is to display the course names on the home.html.

Any help please.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/37caabd1-189b-48c4-9893-dc50660debe3%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Displaying model in a template.

Bhavesh Kumar
Hello,


You must be aware of that choice should be a list or tuple in django model. In tuple it could be represented as (table_value, rendered_value) ,like [('c', 'cat'), ('d', 'dog')]

And I see you have initiated it with a foreign key.

Could you please show the values it is having ?


Regards,
Bhavesh Kumar

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/d1506d3e-fd70-4a56-a3b8-c3ef6756a75a%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Displaying model in a template.

Adam Johnson-2
In reply to this post by Solomon Mbak
Hi!

I think you've found the wrong mailing list for this post. This mailing list is for the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy. Read more on the mailing lists at https://www.djangoproject.com/community/

For support, please use the django-users mailing list, or IRC #django on Freenode, or a site like Stack Overflow. There are people out there willing to help on those channels, but they might not respond if you don't ask your question well. Stack Overflow's question guide can help you frame it well: https://stackoverflow.com/help/how-to-ask .

Also if you haven't read it, please take a look at Django's Code of Conduct: https://www.djangoproject.com/conduct/ . These are our "ground rules" for working well as a community, and will help you get the most out of Django and our fantastic community.

In response to your question - the error message is telling you that the argument "choices" for your category field should not be a foreignkey, but a list - see the documentation and an example in https://docs.djangoproject.com/en/2.2/ref/models/fields/#choices  . The problem is thus in your models and not your view. The Django tutorial uses Question and Choice models and lists them in a template - I think this is similar to what you're trying to do - see https://docs.djangoproject.com/en/2.2/intro/tutorial02/ .

Thanks for your understanding,

Adam

On Sat, 31 Aug 2019 at 00:54, Solomon Mbak <[hidden email]> wrote:
I have a code that looks like this:

class Courses(models.Model):
    CourseName = models.CharField(max_length = 250)
    CourseDescription = models.CharField(max_length = 250)



class Questions(models.Model):
    CAT_CHOICES = models.ForeignKey("main.course_category", on_delete=models.CASCADE)
    question = models.CharField(max_length = 250)
    optiona = models.CharField(max_length = 100)
    optionb = models.CharField(max_length = 100)
    optionc = models.CharField(max_length = 100)
    answer = models.CharField(max_length = 100)
    catagory = models.CharField(max_length=20, choices = CAT_CHOICES)


The work well. The issue is with the views.py (I suspect).

def home(request):
    choices = Questions.CAT_CHOICES
    print(choices)
    return render(request,
        'quiz/home.html',
        {'choices':choices})



ERRORS:
quiz.Questions.catagory: (fields.E004) 'choices' must be an iterable (e.g., a list or tuple).


What I want is to display the course names on the home.html.

Any help please.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/37caabd1-189b-48c4-9893-dc50660debe3%40googlegroups.com.


--
Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMyDDM2fKB-kW5W9Kc-NcwM%3DjD9YSnd0KmipGZJ1h%2BELo6_dKQ%40mail.gmail.com.