nohatekate / what_is_the_plan_p3

0 stars 0 forks source link

Glow Snippet #33

Open nohatekate opened 1 year ago

nohatekate commented 1 year ago

My pride and joy of my app is the random idea functionality. I did a ton of research and tried many different things before making this work. It felt absolutely necessary for my app as this was the base goal (having a random idea chosen for you rather than struggling to choose yourself). I made this happen by using the filter method to get the ideas and then importing random and using the choice method to find the one random idea. I'm just so happy with how this works! It also leads to my favorite part of the detail page where you only have the option to use this functionality if you have more than one idea generated on your page. @login_required def choose_random_idea(request, group_id): group = Group.objects.get(id=group_id) ideas = Idea.objects.filter(group=group_id) random_idea = random.choice(ideas) idea_form = IdeaForm() return render(request, 'groups/detail.html', { 'group': group, 'idea_form': idea_form, 'random_idea': random_idea })