Home > Software design >  How to add two models in one page Django
How to add two models in one page Django

Time:01-10

When i try to add two models in one page it doesn't work and return the html code:enter image description here

How can i add to the one page two models?

views.py

def home(request):
    home_results = MainPageInfo.objects.all();
    context_home = {'home_results': home_results}
    navigation_results_hone = Navigation.objects.all();
    context_navigation_home = {'navigation_results_hone': navigation_results_hone}
    return render(request, 'index.html', context_home, context_navigation_home)

models.py

class Navigation(models.Model):
    title = models.FileField(upload_to='photos/%Y/%m/%d', blank = False, verbose_name=' SVG')

class MainPageInfo(models.Model):
    title = models.CharField(max_length=255, verbose_name='Info')
    

admin.py

admin.site.register(Navigation)

CodePudding user response:

render accepts only one context variable. Therefore, you need to pass everything via one variable:

def home(request):
    home_results = MainPageInfo.objects.all();
    navigation_results_hone = Navigation.objects.all();
    context = {'home_results': home_results, 'navigation_results_hone': navigation_results_hone}
    return render(request, 'index.html', context)
  • Related