{{user}}仅在index.html主页中显示用户。在我的其他页面中,{{用户}}无法正常工作。
我尝试放置一个简单的“测试文本”,并且直到我删除{%extended'locator / base1.html'%}
时,文本也不会显示出来。这是我的base1.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>EME</title>
<!-- Bootstrap core CSS -->
<link href="{% static 'locator/vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<!-- Custom fonts for this template -->
<link href="{% static 'locator/vendor/fontawesome-free/css/all.min.css' %}" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
<!-- Plugin CSS -->
<link href="{% static 'locator/vendor/magnific-popup/magnific-popup.css' %}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="{% static 'locator/css/creative.min.css' %}" rel="stylesheet">
</head>
<body id="page-top">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav1">
<div class="container">
<a class="navbar-brand js-scroll-trigger" href="{% url 'index' %}#page-top">Home</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{% url 'index' %}#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{% url 'index' %}#areas">Areas</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{% url 'index' %}#locate">Church Locator</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{% url 'index' %}#ministries">Ministries</a>
</li>
<li class="nav-item">
{% if user.is_authenticated %}
<a class="nav-link js-scroll-trigger" href="{% url 'profile' %}">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{% url 'logout' %}">Log Out</a>
{% else %}
<a class="nav-link js-scroll-trigger" href="{% url 'register' %}">Register</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{% url 'login' %}">Log In</a>
{% endif %}
</li>
</ul>
</div>
</div>
</nav>
{% block content %}
{% endblock %}
这是我的profile.html
{% extends 'locator/base1.html' %}
{% load staticfiles %}
{% block content %}
{{ user.first_name }}
{{ user.last_name }}
{{ user.country }}
{{ user.city }}
{{ user.email }}
{% endblock %}
这是我的观点。py
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm
from django.contrib.auth.models import User
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Account for {username} has been created! You are now able to log in')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'locator/register.html', {'form': form })
def index(request):
return render(request, 'locator/index.html', {})
def profile(request):
return render(request, 'locator/profile.html', {})
def posts(request):
return render(request, 'locator/posts.html', {})
这是我的urls.py
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('register/', views.register, name='register'),
path('posts/', views.posts, name='posts'),
path('login/', auth_views.LoginView.as_view(template_name='locator/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='locator/logout.html'), name='logout'),
path('profile/', views.profile, name='profile'),
]
答案 0 :(得分:0)
在您看来,您需要像这样将上下文变量传递到模板中:
views.py
def profile(request):
# set variable in a dictionary
context_dict = {'user': request.user}
# pass dictionary to view with render
return render(request, 'locator/profile.html', context_dict)
您可以访问使用context_dict词典传递的任何变量。
我希望这会有所帮助!