【问题标题】:How do I get the Django admin buttons shortcuts to show如何让 Django 管理按钮快捷方式显示
【发布时间】:2016-03-20 10:09:20
【问题描述】:

我目前正在关注 Django 教程,当我进入管理问题区域时,没有日历按钮或今天按钮快捷方式。我不确定问题出在哪里,所以我将在下面列出我配置的文件

views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

admin.py

from django.contrib import admin
from .models import Question

admin.site.register(Question)

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__)))


# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1*%8b@+%9&^5#5x7(yl)kxsa94qxz(tuz6hq%)x^kozg5q'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition
INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'mysite.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/New_York'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = '/static/'

models.py

 import datetime

 from django.db import models
 from django.utils import timezone
 from django.utils.encoding import python_2_unicode_compatible

 # Create your models here.
 @python_2_unicode_compatible
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)


@python_2_unicode_compatible
class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

mysite/urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

polls/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

【问题讨论】:

  • 管理员有样式吗?如果打开浏览器开发工具会怎样?您在控制台中看到错误吗?你的 urls.py 是什么?你这样做了吗:docs.djangoproject.com/en/1.9/howto/static-files/…
  • 检查了控制台,它说“未捕获的 TypeError:$ 不是函数 localhost:/172。但是当我单击 localhost:/172 时什么都没有显示
  • jQuery 未加载。如何启动 Django 开发服务器?我希望 jQuery 生活在localhost:8080/static/admin/something。上面的链接显示了如何在开发模式下提供静态文件。
  • 我是 django 的新手。为什么教程不告诉我?我尝试了你的建议,设置在红色波浪中加了下划线。我不知道放在哪里
  • 我已经读过了,我不能这样做,{% load staticfiles %} 因为这是一个 django 模板。我不知道把这个放在哪里。我是 Django 的新手。教程也没有提到这一步。所以我不认为你的建议是正确的。

标签: python django


【解决方案1】:

事实上,为了显示日期时间选择器和日历,您需要在 django 小部件中调用的内容。 Django 支持内置的date widget,但不是那么先进。

所以最好使用第三方包和jQuery,比如django-datetime-widget

  1. 安装pip install django-datetime-widget

  2. settings.py 中将 datetimewidget 添加到您的 INSTALLED_APPS

  3. forms.py 中:

从 datetimewidget.widgets 导入 DateTimeWidget

class QuestionForm(forms.ModelForm):
    model = Question
    class Meta:
        model = yourModel
        widgets = {
            'datetime': DateTimeWidget(attrs={'id':"yourdatetimeid"}, usel10n = True, bootstrap_version=3)
        }
  1. 在模板文件中
<head>
    ....
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/js/bootstrap.min.js"></script>
    ....
</head>

如果您想在管理界面中使用日期时间小部件,请查看here,它有点老套,需要更多的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2011-01-17
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多