Django:使用模型字段值作为html模板中的有效模板标记

时间:2021-10-20 06:12:21

I have a part on my site where an admin user can create new widget which display in the sidebar on the homepage. I would like to allow users to input code into the form and have the code act as valid code within the template instead of just text. I can't find anything on stack overflow and google which talks about this. So far I have the form and template setup which allows users to add widgets however it is displayed in the page rendering is text instead of executing it as code. Here is what I have so far and what it is doing, I think you'll be able to see what I'm going for based on the code that is being displayed as standard text.

我在我的网站上有一个部分,管理员用户可以创建新的小部件,显示在主页的侧边栏中。我想允许用户在表单中输入代码,并让代码充当模板中的有效代码而不仅仅是文本。我在堆栈溢出和谷歌上找不到任何关于此的内容。到目前为止,我有表单和模板设置,允许用户添加小部件,但它显示在页面呈现文本而不是作为代码执行。这是我到目前为止以及它正在做什么,我认为你将能够根据显示为标准文本的代码看到我的目标。

Adminpanel app models.py:

Adminpanel app models.py:

from django.db import models

# Create your models here.
class Widget(models.Model):
    name = models.CharField(max_length=50)
    widget_order = models.IntegerField(blank=False,unique=True)
    body = models.TextField(max_length=500)

    def __str__(self):
        return self.name

Adminpanel app widget_list_inner.html template:

Adminpanel app widget_list_inner.html模板:

{% for widget in widget_list %}
    <div class="widget">
        <div class="widget-content">
            <p>{{ widget.body }}</p>
        </div>
    </div>
{% endfor %}

Adminpanel app widget_form.html template:

Adminpanel app widget_form.html模板:

{% extends "base.html" %}

{% block content %}
    <div class="colorset-base">
        <h2>Create new widget</h2>
        <form id="postForm" action="{% url 'adminpanel:create-widget' %}" method="POST">
            {% csrf_token %}
            {{ form }}
            <button type="submit" class="submit btn btn-primary btn-large">Add Widget</button>
        </form>
    </div>
{% endblock %}

Not sure if this is relevant but here is my Adminpanel app views.py:

不确定这是否相关,但这是我的Adminpanel应用views.py:

from django.shortcuts import render
from adminpanel.forms import WidgetForm
from adminpanel.models import Widget
from django.utils import timezone

from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse,reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from braces.views import SelectRelatedMixin
from django.views.generic import (TemplateView,ListView,
                                    DetailView,CreateView,
                                    UpdateView,DeleteView)

# Create your views here.
class CreateWidgetView(LoginRequiredMixin,CreateView):
    login_url = '/login/'
    redirect_field_name = 'index.html'
    form_class = WidgetForm
    model = Widget

    def form_valid(self,form):
        self.object = form.save(commit=False)
        self.object.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('adminpanel:widgets')

class SettingsListView(ListView):
    model = Widget
    ordering = ['widget_order']

class DeleteWidget(LoginRequiredMixin,SelectRelatedMixin,DeleteView):
    model = Widget
    select_related = ('Widget',)
    success_url = reverse_lazy('adminpanel:widget')

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(user_id=self.request.user.id)

    def delete(self,*args,**kwargs):
        return super().delete(*args,**kwargs)

Here is what is happening: Django:使用模型字段值作为html模板中的有效模板标记

以下是发生的事情:

As you can see in the sidebar where is says {{ user.username }} <p>{{ user.username }}</p> I want that code to actually execute instead of just display...

您可以在侧边栏中看到{{user.username}}

{{user.username}} 我希望代码实际执行而不是仅显示...

Edit: Here is my index.html where I am using an {% include %} to inject widget_list_inner.html (shown above)

编辑:这是我的index.html,我使用{%include%}来注入widget_list_inner.html(如上所示)

{% extends "base.html" %}

{% block content %}

<div class="container">
    <div class="sidebar">
        {% include "adminpanel/widget_list_inner.html" %}
    </div>

    <div class="content">
    {% for colorset in colorset_list %}
            <div class="colorset-info">
                <h3 class="set-name">{{ colorset.name }}</h3>
                    <p class="author accent-text">Author: {{ colorset.user }}</p>

                    {% if user.is_authenticated and colorset.user == user %}
                        <a class="auth-user-options" href="{% url 'colorsets:delete' pk=colorset.pk %}">Delete</a>
                    {% endif %}
            </div>
            <table class="colorset">
                <tr>
                    <td class="color" style="background-color:#{{ colorset.color_one }}">
                    </td>
                    <td class="color" style="background-color:#{{ colorset.color_two }}">
                    </td>
                    <td class="color" style="background-color:#{{ colorset.color_three }}">
                    </td>
                    <td class="color" style="background-color:#{{ colorset.color_four }}">
                    </td>
                    <td class="color" style="background-color:#{{ colorset.color_five }}">
                    </td>
                </tr>
                <tr>
                    <td>
                        <p>#{{ colorset.color_one }}</p>
                    </td>
                    <td>
                        <p>#{{ colorset.color_two }}</p>
                    </td>
                    <td>
                        <p>#{{ colorset.color_three }}</p>
                    </td>
                    <td>
                        <p>#{{ colorset.color_four }}</p>
                    </td>
                    <td>
                        <p>#{{ colorset.color_five }}</p>
                    </td>
                </tr>
            </table>
    {% endfor %}
    </div>
</div>

{% endblock %}

1 个解决方案

#1


0  

use {{request.user.username}}, the reason?, you have to be specific about what are you asking, so you are making a request and of that request the actual user

使用{{request.user.username}},原因是什么?,你必须具体说明你在问什么,所以你正在向实际用户发出请求和请求

#1


0  

use {{request.user.username}}, the reason?, you have to be specific about what are you asking, so you are making a request and of that request the actual user

使用{{request.user.username}},原因是什么?,你必须具体说明你在问什么,所以你正在向实际用户发出请求和请求