Justin Boucher
Justin Boucher

Reputation: 301

Django view not returning model to template

Please note that I'm new to Django. I created a simple Configuration model, that I want to use to set some global settings on my site. For right now I created just a theme switching setting that replaces the main css file, but I can't get it to load on my login template. Works fine on the index though. Please assist!

Views:

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)

Template on Login.html:

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

Template on Base.html (Working):

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

As a note: I'm not loading the Base.html on my login page, because the user will need to login before seeing anything on the base.html. If I'm doing this the wrong way, please let me know.

Thank you in advance for your assistance.

Edit for 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

Upvotes: 0

Views: 656

Answers (3)

Brandon Taylor
Brandon Taylor

Reputation: 34603

theme_name is missing in the context dictionary you're passing to the template in login_view. You're passing: {'login_form': form}.

You can extend the dictionary provided by get_theme() with additional data, or vice-versa, or you could create a new dictionary from both, and pass that to the template.

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)

Upvotes: 1

Sayse
Sayse

Reputation: 43330

Whilst you are passing the theme into the context incorrectly, it looks like you intend to include this in every view so you'd be better off adding it as a context processor

def theme_name_processor(request):

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

Then add a reference to this to the list of context processors in your settings.

See Writing your own context processors

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 600059

You're not passing the theme correctly. It needs to be in the same dictionary as the form.

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

Upvotes: 1

Related Questions