Django 视图未呈现 - 侧边栏和 header 加载正常但主要内容未加载

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

我经历了很多不同的事情,我就是无法解决;需要一双新眼睛哈哈

我的视图没有呈现,这似乎是我的应用程序和登录页面的情况 - 侧边栏和 header 加载正常,页脚看起来部分加载但我相信这是由于以下事实主要内容未加载。

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 class="row-fluid">
    <div class="col-12">
        <div class="card">
            <div class="class-body">
                <!-- Add New Recipe Form -->
                <form method="POST">
                    {% csrf_token %}
                    <div class="row">
                        <div class="row-fluid pb-1">
                            <!-- Recipe Name -->
                            <div class="mb-3">
                                <!-- 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"]

在您的案例 content.

中,您似乎正在使用 get() 方法将 context 添加到您的视图中

更改您的视图以使用 get_context_data() Django documentation.

例子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
        

您可能还想检查您的 base.html 文件是否包含 {% block contents%} 标签

我的问题出在我的 base.html 文件中 - layout-wrapper 的分隔符似乎没有用 </div>.

关闭

遇到类似问题的任何人,请检查您的 HTML 的主要模板并检查是否所有分隔线都已正确关闭。