Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add two models in one page Django

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading