Django视图不返回模型到模板

时间:2016-12-14 18:16:10

标签: django django-templates django-views

请注意我是Django的新手。我创建了一个简单的配置模型,我想用它在我的网站上设置一些全局设置。现在我只创建了一个替换主css文件的主题切换设置,但我无法在我的登录模板上加载它。虽然在索引上工作正常。请帮忙!

查看:

from django.shortcuts import render, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login, logout
from .forms import LoginForm
from .models import Configuration


def get_theme():
    theme_name = Configuration.objects.only('theme_name').get().theme_name
    context = {'theme_name': theme_name}
    return context


def login_view(request):
    form = LoginForm(request.POST or None)
    if request.POST and form.is_valid():
        user = form.login(request)
        if user:
            login(request, user)
            return HttpResponseRedirect('index')  # Redirect to a success page.
    theme_name = get_theme()
    context = theme_name.update({'login_form': form})
    return render(request, 'registration/login.html', context)


def logout_user(request):
    logout(request)
    return HttpResponseRedirect('registration/login.html')


@login_required(login_url='login/')
def index(request):
    theme_name = get_theme()
    return render(request, 'base.html', theme_name)

Login.html上的模板:

{% compress css %}
    <link href="{% static 'base/css/style-'%}{{ theme_name }}.css" rel="stylesheet" type="text/css">
{% endcompress %}

Base.html上的模板(工作):

{% compress css %}
    <link href="{% static 'base/css/style-'%}{{ theme_name }}.css" rel="stylesheet" type="text/css">
    <link href="{% static 'base/css/sidebar-menu.css' %}" rel="stylesheet" type="text/css">
{% endcompress %}

作为备注:我没有在我的登录页面上加载Base.html,因为用户需要先登录才能看到base.html上的任何内容。如果我这样做的方式不对,请告诉我。

提前感谢您的协助。

编辑forms.py:

class LoginForm(forms.Form):
    username = forms.CharField(max_length=255, required=True)
    password = forms.CharField(widget=forms.PasswordInput, required=True)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if not user or not user.is_active:
            raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
        return self.cleaned_data

    def login(self, request):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        return user

3 个答案:

答案 0 :(得分:1)

您传递给theme_name中的模板的上下文字典中缺少

login_view。你传递的是:{'login_form': form}

您可以使用其他数据扩展get_theme()提供的词典,反之亦然,或者您可以从两者创建新词典,并将其传递给模板。

def login_view(request):
    form = LoginForm(request.POST or None)
    if request.POST and form.is_valid():
        user = form.login(request)
        if user:
            login(request, user)
            return HttpResponseRedirect('index')  # Redirect to a success page.
    theme_name = get_theme()
    context = theme_name.update({'login_form': form})
    return render(request, 'registration/login.html', context)

答案 1 :(得分:1)

您没有正确传递主题。它需要与表单在同一个字典中。

return render(request, 'registration/login.html', {'login_form': form, 'theme_name': theme_name})

答案 2 :(得分:1)

虽然您错误地将主题传递到上下文中,但您似乎打算在每个视图中包含此内容,因此您最好将其添加为上下文处理器

import cv2
import numpy as np

img = cv2.imread('image.jpg',0)

# Applies threshold and inverts the image colors
(thresh, im_bw) = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
im_wb = (255-im_bw)

# Line parameters
minLineLength = 100
maxLineGap = 10
color = 255
size = 2

# Substracts the black line
lines = cv2.HoughLinesP(im_wb,1,np.pi/180,minLineLength,maxLineGap)[0]
for x1,y1,x2,y2 in lines:
    cv2.line(img,(x1,y1),(x2,y2),color,size) 

cv2.imshow('clean', img)

然后将此引用添加到设置中的上下文处理器列表中。

请参阅Writing your own context processors