如何使 jquery、css 外部和内部样式在我的模板中起作用?
How can i make jquery, css external and internal styling work in my template?
我从事 django 项目已经有一段时间了,但是我在使外部和内部 css 工作时遇到了一些问题,只有内联 css 工作,我有 2 个问题
我怎样才能使外部 css 或内部 css 工作并且 jquery?我尝试了 运行 collectstatic 命令,我添加了 STATIC_URL、STATIC_DIR 等,我还添加了 css 和 jquery 文件路径到我的 html
如何美化我的 registration/login 表单?我已经尝试了 django_crispy_forms 但我仍然没有得到想要的结果。
在此先感谢您的帮助。
settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
MEDIA_DIR = os.path.join(BASE_DIR, "media")
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
AUTH_USER_MODEL = "website.User"
"""STATICFILES_DIR = [
STATIC_DIR,
]"""
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#My Apps
#'avatar',
'django_countries',
'website',
'djmoney',
'crispy_forms',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'main.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'main.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
USE_THOUSAND_SEPARATOR = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
main html file
<!DOCTYPE html>
<html lang="en-US" style="height: 100%;">
<head>
{% load static %}
{% load filter %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" >
<!-- Prism CSS -->
<!---<link href="{% static "tinymce/css/prism.css" %}" rel="stylesheet"> -->
<link rel="stylesheet" type="text/css" href="{% static 'css/header.css' %}"/>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="/static/website/css/header.css" type="text/css" media="screen" >
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-light ">
<a class="navbar-brand" href="{% url 'website:homepage' %}">Smart X</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end " id="navbarSupportedContent">
<ul class="navbar-nav " >
<li class="nav-item active ">
<a class="nav-link " href="/">Home <span class="sr-only">(current)</span></a>
</li>
{% if user.is_authenticated %}
<li class="nav-item"><a class="nav-link" href="/account"><strong>Account</strong></a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Track Expense
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'website:create-expense' %}">Create Expense</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'website:check-expense' %}">Check Expense</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Budget
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'website:create-budget' %}">Create Budget</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'website:check-budget' %}">Check Budget</a>
</div>
</li>
<li class="nav-item"><a class="nav-link" href="/contact"><strong>Contact</strong></a></li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle btn" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ request.user.last_name }} {{ request.user.email|gravatar:30 }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'website:profile' %}">Profile</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'website:settings' %}">Setting</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="/logout"><strong>Logout</strong></a>
</div>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/contact"><strong>Contact</strong></a></li>
<li class="nav-item"><a class="nav-link" href="/login"><strong>Login</strong></a></li>
<li class="nav-item"><a class="nav-link" href="/register"><strong>Register</strong></a></li>
{% endif %}
</ul>
</div>
</nav>
<body style="position: relative;
margin: 0;
padding-bottom: 6rem;
min-height: 100%; ">
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">
{% if 'safe' in message.tags %}
{{ message|safe }}
{% else %}
{{ message }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% block content %}
{% endblock %}
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</body>
</html>
my registration page html
{% extends "website/header.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="col-md-8 offset-md-2" style="margin-top: 50px; border-style: groove;">
<h1>Create New Account</h1>
<form method = "POST">
{% csrf_token %}
<div class="form-group">
{{ form.first_name | as_crispy_field }}
</div>
<div class="form-group">
{{ form.last_name | as_crispy_field }}
</div>
<div class="form-group">
{{ form.email | as_crispy_field }}
</div>
<div class="form-group">
{{ form.password1 | as_crispy_field }}
</div>
<div class="form-group">
{{ form.password2 | as_crispy_field }}
</div>
<button style="background-color: lightblue; color: black; " type="submit">SIGN
UP</button>
</form>
<p><strong>Already have an account? <a href="/login"><strong>Login</strong></a></strong></p>
</div>
{% endblock %}
This is what my login page looks like
My desired result, i want the login page and registration to look this way
在您的静态文件夹中创建一个 css 文件夹,并在其中添加您的 css 文件。 Link 它在你的 base.html 文件中,如下所示在 head 标签内(注意:我将 css 文件命名为 custom.css)
<link rel="stylesheet" type="text/css" href="{% static 'css/custom.css' %}"/>
并确保在您的设置文件中进行了以下设置
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
CRISPY_TEMPLATE_PACK = 'bootstrap4'
我从事 django 项目已经有一段时间了,但是我在使外部和内部 css 工作时遇到了一些问题,只有内联 css 工作,我有 2 个问题
我怎样才能使外部 css 或内部 css 工作并且 jquery?我尝试了 运行 collectstatic 命令,我添加了 STATIC_URL、STATIC_DIR 等,我还添加了 css 和 jquery 文件路径到我的 html
如何美化我的 registration/login 表单?我已经尝试了 django_crispy_forms 但我仍然没有得到想要的结果。
在此先感谢您的帮助。
settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
MEDIA_DIR = os.path.join(BASE_DIR, "media")
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
AUTH_USER_MODEL = "website.User"
"""STATICFILES_DIR = [
STATIC_DIR,
]"""
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#My Apps
#'avatar',
'django_countries',
'website',
'djmoney',
'crispy_forms',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'main.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'main.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
USE_THOUSAND_SEPARATOR = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
main html file
<!DOCTYPE html>
<html lang="en-US" style="height: 100%;">
<head>
{% load static %}
{% load filter %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" >
<!-- Prism CSS -->
<!---<link href="{% static "tinymce/css/prism.css" %}" rel="stylesheet"> -->
<link rel="stylesheet" type="text/css" href="{% static 'css/header.css' %}"/>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="/static/website/css/header.css" type="text/css" media="screen" >
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-light ">
<a class="navbar-brand" href="{% url 'website:homepage' %}">Smart X</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end " id="navbarSupportedContent">
<ul class="navbar-nav " >
<li class="nav-item active ">
<a class="nav-link " href="/">Home <span class="sr-only">(current)</span></a>
</li>
{% if user.is_authenticated %}
<li class="nav-item"><a class="nav-link" href="/account"><strong>Account</strong></a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Track Expense
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'website:create-expense' %}">Create Expense</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'website:check-expense' %}">Check Expense</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Budget
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'website:create-budget' %}">Create Budget</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'website:check-budget' %}">Check Budget</a>
</div>
</li>
<li class="nav-item"><a class="nav-link" href="/contact"><strong>Contact</strong></a></li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle btn" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ request.user.last_name }} {{ request.user.email|gravatar:30 }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{% url 'website:profile' %}">Profile</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'website:settings' %}">Setting</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="/logout"><strong>Logout</strong></a>
</div>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/contact"><strong>Contact</strong></a></li>
<li class="nav-item"><a class="nav-link" href="/login"><strong>Login</strong></a></li>
<li class="nav-item"><a class="nav-link" href="/register"><strong>Register</strong></a></li>
{% endif %}
</ul>
</div>
</nav>
<body style="position: relative;
margin: 0;
padding-bottom: 6rem;
min-height: 100%; ">
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li class="{{ message.tags }}">
{% if 'safe' in message.tags %}
{{ message|safe }}
{% else %}
{{ message }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% block content %}
{% endblock %}
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</body>
</html>
my registration page html
{% extends "website/header.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="col-md-8 offset-md-2" style="margin-top: 50px; border-style: groove;">
<h1>Create New Account</h1>
<form method = "POST">
{% csrf_token %}
<div class="form-group">
{{ form.first_name | as_crispy_field }}
</div>
<div class="form-group">
{{ form.last_name | as_crispy_field }}
</div>
<div class="form-group">
{{ form.email | as_crispy_field }}
</div>
<div class="form-group">
{{ form.password1 | as_crispy_field }}
</div>
<div class="form-group">
{{ form.password2 | as_crispy_field }}
</div>
<button style="background-color: lightblue; color: black; " type="submit">SIGN
UP</button>
</form>
<p><strong>Already have an account? <a href="/login"><strong>Login</strong></a></strong></p>
</div>
{% endblock %}
This is what my login page looks like
My desired result, i want the login page and registration to look this way
在您的静态文件夹中创建一个 css 文件夹,并在其中添加您的 css 文件。 Link 它在你的 base.html 文件中,如下所示在 head 标签内(注意:我将 css 文件命名为 custom.css)
<link rel="stylesheet" type="text/css" href="{% static 'css/custom.css' %}"/>
并确保在您的设置文件中进行了以下设置
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
CRISPY_TEMPLATE_PACK = 'bootstrap4'