我有以下问题。在login.html中,我添加了{% If user.is_authenticated %}
,一切正常。我还在navbar.html和home.html中添加了这个,但是它没有用,我也不知道为什么。
这是我的views.py
def index(request):
return render(request, 'website/home.html', {'user': user_panel})
def register(request):
registered = False
if request.method =='POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'picture' in request.FILES:
profile.picture = request.FILES['picture']
profile.save()
registered = True
else:
print (user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request, 'website/register.html', {
'user_form': user_form,
'profile_form': profile_form,
'registered': registered})
def auth_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("Konto jest nieaktywne")
else:
print ("Invalid login details: {0}, {1}".format(username, password))
return HttpResponse("Invalid login details supplied.")
else:
return render(request, 'website/login.html', {})
basic.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}" type = "text/css"/>
<link rel="stylesheet" href="{% static 'css/style.css' %}" type = "text/css"/>
<link href='https://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
</head>
<body>
{% include 'website/navbar.html' %}
{% block body_block %}
{% endblock %}
</body>
</html>
home.html的
{% extends 'website/basic.html' %}
{% block body_block %}
{% load staticfiles %}
<div class="container-full-bg" style="background-image:url('{% static 'images/website/jumbotron-website.PNG' %}');" height="100%">
<div class="jumbotron" style="background: transparent">
<div class="container" style="margin-top: 6em"><h1>Agencja Reklamy FUX</h1>
<p>Nasza Agencja to firma z ponad dwudziestoletnią tradycją. Zajmujemy się głównie wielkoformatową
reklamą zewnętrzną, dającą niemalże nieograniczone możliwości przekazu.</p></div>
{% if user.is_authenticated %}
Hello {{ user.username }}
{% else %}
You are not loggin in
{% endif %}
</div>
</div>
{% endblock %}
navbar.html
{% load staticfiles %}
<nav class="navbar navbar-inverse navbar-fixed-top" style="background-color: #363636;">
<div class="container-fluid">
<div class = "container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" style="padding: 0em"><img src="{% static 'images/website/logo.png' %}"></img></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="{% url 'index' %}">Home <span class="sr-only">(current)</span></a></li>
<li><a href="#">O Firmie</a></li>
<li><a href="#">Oferta</a></li>
<li><a href="#">Klienci</a></li>
<li><a href="#">Praca</a></li>
<li><a href="#">Kontakt</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'logout' %}" class="btn btn-primary" role="button">Wyloguj się</a></li>
{% else %}
<li><a href="{% url 'login' %}" class="btn btn-primary" role="button">Zaloguj się</a></li>
{% endif %}
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</div>
</nav>
的login.html
{% extends 'website/basic.html' %}
{% block body_block %}
<div class="container" style="margin-top: 8rem">
{% if not user.is_authenticated %}
<form id="login_form" method="post" action="/login/">
{% csrf_token %}
<div class="form-group" style="padding-right: 80rem">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" size="60" />
</div>
<div class="form-group" style="padding-right: 80rem">
<label for="username">Password</label>
<input type="password" class="form-control" name="password" size="60" />
</div>
<div class="form-group" style="padding-right: 80rem">
<button type="submit" class="btn btn-default" value="submit">Submit</button>
</div>
</form>
{% else %}
You are logged! {{ user.username }}
{% endif %}
</div>
{% endblock %}