How to set a background image for the Django login page?
To set a background image in the login page of Django, you can follow these steps:
- Create a folder named “css” in the static files directory of your Django project (if it doesn’t already exist).
- Create a file named “login.css” in the “css” folder.
- Add the following code to the “login.css” file to set the background image:
body {
background-image: url('/static/images/login_background.jpg');
background-repeat: no-repeat;
background-size: cover;
}
- Place your background image (e.g. “login_background.jpg”) into the “images” folder in the static files folder (if it doesn’t exist).
- In the login view function of Django, add the “login.css” file to the HTML template of the login page. For example:
from django.shortcuts import render
def login_view(request):
return render(request, 'login.html', {'css': 'css/login.css'})
- To load the “login.css” file, add the following code to your login page template (such as “login.html”).
{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static css %}">
</head>
<body>
<!-- 登录表单和其他内容 -->
</body>
</html>
- When you run the Django server and access the login page, you should be able to see the background image that was set.
Please note that the file paths and filenames in the above steps may need to be adjusted according to your project’s file structure.