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

时间:2017-09-02 22:34:29

标签: python django templates

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

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模板:

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

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 %}

不确定这是否相关,但这是我的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)

以下是发生的事情: enter image description here

正如您在侧边栏中看到的那样{{ user.username }} <p>{{ user.username }}</p>我希望代码实际执行而不仅仅是显示......

修改:这是我的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 个答案:

答案 0 :(得分:0)

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