简体   繁体   中英

Fetch data from database based on choices django

I am creating Artist booking web app and I have a dropdown list of artists like celeb, dancer, model on navbar so when I click any of them it should render that list from the database. I'm storing the artist based on choices

This is my models.py:

from django.db import models

class Artist(models.Model):
   CHOICES = (
    (0, 'celebrities'),
    (1, 'singer'),
    (2, 'comedian'),
    (3, 'dancer'),
    (4, 'model'),
    (5, 'Photographer')
)
artist_name = models.CharField(max_length=50)
artist_type = models.IntegerField(choices=CHOICES)
description = models.TextField(max_length=500)

def __str__(self):
    return self.artist_name

views.py:

def talent(request):
    artists = Artist.objects.all()
    context = {
        'aka': artists
    }
    return render(request, 'main_site/talent.html', context)

when I am rendering this, it gets all the lists but I need a specific list like when I click on dancer it should get dancer only.

This is base.html navbar:

<ul class="menu-horizontal text-left ">
    <li><a href={% url 'mainsite-index'%} class="menu-horizontal text-left nav-font">Home</a></li>
    <li><a href={% url 'mainsite-about'%} class="menu-horizontal text-left nav-font">About</a></li>
    <li class="dropdown"> <a href="#" class="menu-horizontal text-left dropdown-toggle nav-font" data-toggle="dropdown">Artist</a></li>
</ul>

<ul class="dropdown-menu nav-font" style="margin-top: 7px">
    <li> <a href={% url 'mainsite-talent'  %} style="color:grey;padding-left:5px">Celebrities</a></li>
    <li> <a href={% url 'mainsite-talent' %} style="color:grey;padding-left:5px">Singer</a></li>
    <li> <a href={% url 'mainsite-talent' %} style="color:grey;padding-left:5px">Comedian</a></li>
    <li> <a href={% url 'mainsite-talent' %} style="color:grey;padding-left:5px">Dancer</a></li>
    <li> <a href={% url 'mainsite-talent' %} style="color:grey;padding-left:5px">Model</a></li>
    <li> <a href={% url 'mainsite-talent' %} style="color:grey;padding-left:5px">Photographer</a></li>                            
</ul>

urls.py:

from django.urls import path
from. import views

urlpatterns = [
  path('',views.index, name='mainsite-index'),
  path('about/',views.about, name='mainsite-about'),
  path('contact/', views.contact, name='mainsite-contact'),
  path('artist/',views.talent, name='mainsite-talent'),
  path('book_artist/', views.artist_booking, name='artist_book')
]

talent.html:

{% for talent in aka %}
   <h2>{{ talent.artist_name }}</h2>
{% endfor %}

You can filter on django ORM using " filter " You'll probably need to send the type of artist to the view as a parameter.

urls:

path('artist/<int:artist_type>/',views.talent, name='mainsite-talent'),

views.py

def talent(request, artist_type):
    artists = Artist.objects.filter(artist_type=artist_type)
    context = {
        'aka': artists
    }
    return render(request, 'main_site/talent.html', context)

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