Home > Mobile >  Django view not rendering - sidebar & header loads fine but main content does not load
Django view not rendering - sidebar & header loads fine but main content does not load

Time:05-18

I've been through so many different things and I just can't work it out; need a fresh set of eyes haha

My view does not render and this seems to be the case across my apps and landing page - the sidebar and header loads fine, footer looks to partially load but I believe this is due to the fact the main content does not load.

Current HTML page screenshot - not rendering view

views.py

from django.shortcuts import redirect, render
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.paginator import Paginator
from django.http.response import HttpResponse

# Form Imports
from .models import NewRecipeForm

#########################################  
# Form Pages                            #
#########################################

# Recipe View
class RecipesView(LoginRequiredMixin, View):
    def get(self, request):
        content = {}
        content['heading'] = "Recipes Database"
        content['pageview'] = "Recipes & Formulations"
        return render(request, 'nf_recipes/recipes-database.html', content)

#########################################  
# Form Pages                            #
#########################################

# Add New Recipe Form
class AddNewRecipeForm(LoginRequiredMixin, View):
    def get(self, request):
        add_new_recipe_form = NewRecipeForm()
        content = {}
        content['heading'] = "Add New Recipe"
        content['pageview'] = "Recipes & Formulations"
        content['add_new_recipe_form'] = add_new_recipe_form
        return render(request, 'nf_recipes/add-new-recipe.html', content)
    
    def post(self, request):
        if "add-new-recipe-submit" in request.POST:
            add_new_recipe_form = NewRecipeForm(request.POST)
            add_new_recipe_form.save()
            return redirect("/nf_recipes/recipe-db")
# End of Add New Recipe Form

add-new-recipe.html

{% extends 'partials/base.html' %}
{% load static %}
{% load humanize %}
{% load crispy_forms_tags %}

{% block contents %}
<div >
    <div >
        <div >
            <div >
                <!-- Add New Recipe Form -->
                <form method="POST">
                    {% csrf_token %}
                    <div >
                        <div >
                            <!-- Recipe Name -->
                            <div >
                                <!-- Test page content -->
                                <h3>This is a test</h3>
                            </div>
                        </div>
                    </div>
                </form>
                <!-- End of Add New Recipe Form -->
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_javascript %}
{% endblock %}

models.py

import datetime
from django.db import models
from django import forms
from django.forms.widgets import TextInput, Textarea, DateInput
from django.contrib import admin
from django.contrib.auth.models import User

#########################################  
# Static Datasets                       #
#########################################

# Unit Measurement Sets
unit_measure = [
    (0,'g'),
    (1,'kg'),
    (2,'cup'),
    (3,'cups'),
    (4,'tsp'),
    (5,'tbsp'),
    (6,'cl'),
    (7,'ml'),
    (8,'l')
]

#########################################  
# Models                                #
#########################################

# New Recipe Model
class Recipes(models.Model):
    recipe_idpk = models.AutoField(primary_key=True)
    recipe_name = models.CharField(max_length=250, verbose_name="Recipe Name")
    recipe_date = models.DateField(auto_now_add=True, verbose_name="Recipe Addition Date")
    recipe_description = models.TextField(verbose_name="Recipe Description")

    def __str__(self):
        return f"{self.recipe_name}"

    class Meta:
        ordering = ['-recipe_date']
        verbose_name = 'Recipes'
        verbose_name_plural = 'Recipes'

#########################################  
# Forms                                 #
#########################################

# New Recipe Form
class NewRecipeForm(forms.ModelForm):
    class Meta:
        model = Recipes
        fields = ["recipe_name","recipe_description"]

CodePudding user response:

it seems that you're using the get() method to add the context to your views in your case content.

change your view to use get_context_data() Django documentation.

example views.py

# Recipe View
class RecipesView(LoginRequiredMixin, TemplateView):
    template_name = 'nf_recipes/recipes-database.html'

    def get_context_data(self, **kwargs):
        context = super(RecipesView, self).get_context_data(**kwargs)

        context.update({
            'heading': "Recipes Database",
            'pageview': "Recipes & Formulations"
        })

        return context
        

You might also want to check that your base.html file has the {% block contents%} tag in it

CodePudding user response:

My issue was in my base.html file - it appeared that my divider for layout-wrapper was not closed with </div>.

Anyone with similar issues, review your primary templates for your HTML and check to see if all dividers have been closed correctly.

  • Related