M
M
Mike2017-07-12 13:22:26
Django
Mike, 2017-07-12 13:22:26

How to do not repeat?

I have two models
file models.py

from django.db import models
# -*- coding: UTF-8 -*-

class LessonOne(models.Model):
    russia = models.CharField(max_length=250)
    english = models.CharField(max_length=250)
    buttons = models.TextField()
    def __str__(self):
        return self.russia

class LessonTwo(models.Model):
    russia = models.CharField(max_length=250)
    english = models.CharField(max_length=250)
    buttons = models.TextField()
    def __str__(self):
        return self.russia'

In views.py file
def listdataOne(request):
    data = LessonOne.objects.all()
    return render(request, 'fighter/listdataOne.html', {'data': data, "amount": len(data)})

def listdataTwo(request):
    data = LessonTwo.objects.all()
    return render(request, 'fighter/listdataTwo.html', {'data': data, "amount": len(data)})

In index.html
<li><a href="/fighter/listdataOne">First lesson.</a></li>
<li><a href="/fighter/listdataTwo">Second lesson.</a></li>

Is it possible somehow to replace the listdataOne and listdataTwo functions with one function and make one HTML template for it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tema_sun, 2017-07-12
@tema_sun

# -*- coding: UTF-8 -*-
Should be the very first line.
You need one model:

class Lesson(models.Model):
    lesson_number = models.IntegerField()
    russia = models.CharField(max_length=250)
    english = models.CharField(max_length=250)
    buttons = models.TextField()
    def __str__(self):
        return self.russia

And, accordingly, one function for the output:
def listdata(request, lesson_number):
    data = LessonOne.objects.filter(lesson_number=lesson_number )
    return render(request, 'fighter/listdataOne.html', {'data': data, "amount": len(data)})

But judging by the question, I can conclude that you have not studied the official tutorial. Start from there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question