Hello! I can’t solve the following problem for a very long time: I need to implement a lesson schedule on the django website. that is, it should look like this - there is a drop-down list with classes on the page (5 "A", 5 "B" .... 11 "C") and the "Show" button. When selecting an item from the drop-down list, the weekly lesson schedule for the selected class should open. Ideally, implementation without a redirect, but I think I’m not sure about this, because at the moment I can’t do this with changing the page. So, I have the following: In models.py :

class Classes(models.Model): class Meta: db_table = 'classes' verbose_name_plural = "Классы" verbose_name = "Класс" name = models.CharField(max_length=50, unique=True, verbose_name = 'Классы') def __str__(self): return self.name class Lessons(models.Model): class Meta(): db_table = "lessons" verbose_name_plural = "Уроки" verbose_name = "Уроки" lessons_class = models.ForeignKey(Classes) lessons_creationdate = models.DateField() lessons_weekcontent = RichTextField() 

Accordingly, in the admin panel I manually enter classes (5 "A" ...) and so on, and then manually enter the lesson schedule for the week and choose which class it belongs to. Further, in forms.py:

 class LessonsForm(forms.ModelForm): classname = forms.ModelChoiceField(queryset = Classes.objects.all(),empty_label="Выберите класс",widget=forms.Select(attrs={'class':'dropdown'}),label="Класс") class Meta: model = Lessons fields = ('lessons_class',) 

I read the django documentation a lot of times, but I still dimly imagine how views.py should look like . I have the following:

 def LessonsShow(request): if request.method == 'POST': form = LessonsForm(request.POST) if form.is_valid(): return HttpResponseRedirect('/about/') else: form = LessonsForm() return render(request, 'timetable.html', {'form': form}) 

I understand that I should have some kind of processing after if form.is_valid () :, but I can’t imagine what should be there. Finally, in the timetable.html template :

 {% extends 'base.html' %} {% block content %} <form method="post"> {% csrf_token %} {{ form.classname }} <input type="submit" value="Показать расписание"> </form> {% endblock %} 

As a result, on the page http://127.0.0.1:8000/timetable/ I have a drop-down list with classes from my database, but when I select one of them and press a button, nothing happens. What can be done so that when choosing the value of this drop-down list and clicking on the "Show schedule" button, the appropriate table with the class schedule for the week opens for this class? I hope very much for your help.

  • Why do you need a form? Make a View for the timetable by class, and on the class selection page, list them with links. - Chikiro
  • If you want the form, then in the form instead of action = "POST", put action = "GET". And in the View of viewing the schedule for the week, take the GET and filter the queryset by its parameters. Now it is not entirely clear what specific result is needed: a page with ajax loading, separate pages of schedules for a week by classes, one large schedule with the ability to filter it by parameters? - Chikiro
  • We need separate pages of schedules. Thanks for the tips) - Aswz

1 answer 1

In the comments, the solution is actually found. If you do without js, then the easiest way is through a form sent by GET.

After pressing the button, a new window opens, with the parameters passed to the url. Not a seo url, but you don’t need it here.

 /timetable/?name=7Б 

in views we parse data from GET

 klass = request.GET['name'] Classes.objects.get(name=klass) 

Next, the template output as needed.

One page, no redirects, you can give a link, go from bookmarks.

If you really want "beautiful" url, then you need to immediately go from the list to the page you need, for this javascript code is used that opens it.

Either the same redirect, but then you will need to write another processing function or in the current check for the presence of parameters.

The main thing is to be able to click on the link to the desired class, albeit from bookmarks. Nobody learns at once in the whole school, and choosing each time from the list can be annoying.