#🔒 Django is there any way of a django view to update the template connected to another view rather

14 messages · Page 1 of 1 (latest)

clever panther
#

hello i am learning and trying out features in django am on adding this category feature in different ways through a model, through a field and also through get requests...

median oreBOT
#

@clever panther

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

clever panther
#
from django.db import models
from django.contrib.auth.models import User
from django.utils.text import slugify
# Create your models here.

class WorkshopCategory(models.Model):
    name = models.CharField(max_length=200)
    workshopcategoryslug = models.SlugField(null=True)


    class Meta:
        verbose_name_plural = "WorkshopCategories"

    
    def save(self,*args, **kwargs):
        self.workshopcategoryslug = slugify(self.name)

        super().save(*args, **kwargs)


    def __str__(self):
        return self.name

class Instructor(models.Model):
    name = models.CharField(max_length=120)
    role = models.CharField(max_length=120)
    qualification = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Workshop(models.Model):
    name = models.CharField(max_length=120)
    description = models.TextField(max_length=2000)
    date = models.DateField(auto_now_add=True)
    image = models.ImageField(null = True,blank=True)
    students_in_workshop = models.IntegerField() #
    workshopcategory = models.ForeignKey(WorkshopCategory, on_delete=models.CASCADE) #
    difficulty_choices = models.TextChoices("difficulty_choices","Easy Intermediate Difficult")
    difficulty = models.CharField(choices=difficulty_choices,max_length=50)
    paid = models.BooleanField(default=False)
    instructor = models.ForeignKey(Instructor, on_delete=models.CASCADE)
    workshopslug = models.SlugField(null=True)


    def save(self, *args, **kwargs):
        self.workshopslug = slugify(self.name)

        super().save(*args, **kwargs)



    def __str__(self):
        return self.name

class Enrollment(models.Model):
    student = models.ForeignKey(User, on_delete=models.CASCADE)
    workshop = models.ForeignKey(Workshop, on_delete=models.CASCADE)


    def __str__(self):
        return f"{self.student} joined {self.workshop}"



class Profile(models.Model):
    pass``` this is my models
#

def all_workshops(request):
    all_workshops = Workshop.objects.all()
    ctx = {"all_workshops":all_workshops}
    return render(request,"workshop/all_workshops.html",context=ctx)

def workshop_detail(request,slugworkshop):
    workshop = Workshop.objects.get(workshopslug=slugworkshop)
    ctx = {"workshop":workshop}
    return render(request,"workshop/workshop_detail.html",context=ctx)

def related_workshops(request,slugworkshop):
    """
    gets the selected workshop and filter and renders the allworkshops
    """
    workshop_selected = Workshop.objects.get(workshopslug=slugworkshop)
    category_of_workshop = workshop_selected.workshopcategory
    workshops_of_category = Workshop.objects.filter(workshopcategory=category_of_workshop)
    ctx = {"all_workshops":workshops_of_category}
    return render(request,"workshop/all_workshops.html",context=ctx)

def category(request):
    difficulty_levels = Workshop.objects.order_by().values_list('difficulty', flat=True).distinct()# or Workshop.difficulty_choices.choices
    print(difficulty_levels)
    categories = WorkshopCategory.objects.all()
    all_workshops = Workshop.objects.all()   
    ctx = {"categories":categories,"all_workshops":all_workshops,"difficulty":difficulty_levels}
    return render(request,"workshop/workshop_category.html",context=ctx)


def category_details(request,slugworkshopcategory):
    categories = WorkshopCategory.objects.all()
    category = WorkshopCategory.objects.get(workshopcategoryslug=slugworkshopcategory)
    workshops = category.workshop_set.all()
    ctx = {"all_workshops":workshops,"categories":categories}
    return render(request,"workshop/workshop_category.html",context=ctx)


def category_paid_difficulty(request):
    
    difficulty = request.GET["difficulty"]
    workshopbydifficulty = Workshop.objects.filter(difficulty=difficulty)

    ``` my views
#

now i successfully implemented categories with a model and with a field of that model

#

i am now adding it via a get request

#

but html {% block body %} {% for diff in difficulty %} <a href="{% url "categoryby" %}?difficulty={{diff}}">{{diff}}</a> {% endfor %}

#

i am using a seperate url and view for each so now am again forced to re render the category page

#

is there a alternative

#

like in my category_paid_difficulty can give the filtered data to category

#

its too much re rendering

#

if theres no other way than using a single view for all or using js pls let me know

median oreBOT
#

@clever panther

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.