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.