简体   繁体   中英

Why is this not displaying properly?

I am really not sure why this isn't displaying the band names with their hyperlinks, all that is appearing is the title 'Band List' with nothing beneath it.

There is data in the database but nothing is showing.

Here is the template.

{% block content %}
<h1>Band List</h1>
{% for band in bands %}
{{ band.id }}
<h3><a href='/bandlist/{{band.id}}'>{{ band.bandname }}</a></h3>
{% endfor %}

{% endblock %}

Here are my views.py

from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render, render_to_response, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from polls.forms import NormalSignUpForm, VenueSignUpForm, BLSignUpForm, BMSignUpForm, ProfileForm
from django.contrib.auth import get_user_model
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import UpdateView, TemplateView
from django.utils.decorators import method_decorator
from django.db.models.signals import post_save
from .models import *
from polls.models import User
from django.http import Http404

def bandlist(request):
    query = Band.objects.all()
    print(query)
    args =  {'query': query}
    return render(request, 'bandlist.html', args)

Here are my models.py

class Band(models.Model):
    bandname = models.CharField(max_length = 50)   
    description = models.TextField()
    picture = models.ImageField()
    members = models.ManyToManyField('User')

    def __str__(self):
        return self.bandname

If anyone could provide clarification on this it would be really helpful, thank you.

This line return render(request, 'bandlist.html', args) sends a context object called args to the template. So your template code should have:

{% for band in query %}
    {{ band.id }}
    <h3><a href='/bandlist/{{band.id}}'>{{ band.bandname }}</a></h3>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM