0

I am currently following the Django tutorial and when I get to the admin question area, there are no calendar button or today button shortcuts. I'm not sure where the problem is, so I'll list the files I've configured below

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'),
]
Dhia
  • 10,119
  • 11
  • 58
  • 69
losee
  • 2,190
  • 3
  • 29
  • 55
  • Is the the admin styled at all? What if you open the browsers development tools? Do you see errors in the console? What are your urls.py? Did you do this: https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-static-files-during-development – allcaps Dec 14 '15 at 20:06
  • Checked the console and it says "Uncaught TypeError: $ is not a function localhost:/172. But when I click localhost:/172 nothing shows – losee Dec 14 '15 at 20:10
  • jQuery isn't loaded. How do you start the Django development server? I expect jQuery to live at `localhost:8080/static/admin/something`. The above link shows how you can serve static files in development mode. – allcaps Dec 14 '15 at 20:35
  • I am new to django. Why would the tutorial not tell me that? I tried what you suggested and settings was underlined in red squiggly's. i do not know where to put it – losee Dec 14 '15 at 22:03
  • I've read it over and I cant do something like this, {% load staticfiles %} My image because this is a django template. I don't know where to put this. I am new to django. also the tuttorial doesn't mention this step. So I don't think your suggestion is right. – losee Dec 14 '15 at 22:10
  • based on stackoverflow rules of how to ask a question, why was this downvoted? I asked a question with the accompanying code and asked for a suggestion. – losee Dec 14 '15 at 22:52
  • You Q might be down voted because SO is full with questions like yours. So it is a duplicate. But I think you supply enough info. We where all beginners once. I didn't do the tutorial recently, but you are right. It should mention it and/or be sufficient as is. That said, it is very likely that you did something different than the tut. What is hard to tell. – allcaps Dec 14 '15 at 23:49
  • Is the admin styled at all? – allcaps Dec 14 '15 at 23:57
  • yes the admin loks the same as the tut minus the month and now icons – losee Dec 15 '15 at 00:03
  • Yes, and you have an $ is not a function error in the browser console. Can you see other 404's in the browser webdeveloper tools network tab? It is weird that it looks for localhost. The default dev server runs on 127.0.0.1:8000. Are there errors in your terminal (where you run `python manage.py runserver`)? – allcaps Dec 15 '15 at 00:20
  • I just did the tutorial and the icons show up. So Django serves the icons by default. It also looks like all the code you posted is right. FYI the icon should be accessible via http://127.0.0.1:8000/static/admin/img/icon-calendar.svg – allcaps Dec 15 '15 at 00:31
  • what browser are you using? I was using chrome and safari. And I saw them when I clicked the link you posted. – losee Dec 15 '15 at 00:46
  • Chrome. But any modern browser is okay. Smart to check various browsers. That rules out that your browser is the issue. So the icon is served. But since you have an `$ is not a function` that means there is still something wrong with jQuery. Can you reload the page and post the contents of your browser console? – allcaps Dec 15 '15 at 08:30
  • @allcaps I'm glad you tried to help me. I ended up scrapping it and doing it all over and it works. Dont know whats different but it works now – losee Dec 15 '15 at 20:24

1 Answers1

0

In fact in order to display datetime picker and calender you need what is called in django widget. Django has support to built-in date widget but it's not so advanced.

So it's better to use third-party package with jQuery like django-datetime-widget.

  1. install pip install django-datetime-widget

  2. add datetimewidget to your INSTALLED_APPS in settings.py.

  3. in forms.py:

from datetimewidget.widgets import DateTimeWidget

class QuestionForm(forms.ModelForm):
    model = Question
    class Meta:
        model = yourModel
        widgets = {
            'datetime': DateTimeWidget(attrs={'id':"yourdatetimeid"}, usel10n = True, bootstrap_version=3)
        }
  1. in template file
<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>

In case you want to have the datetime widget in your admin interface so check here, it's a little bit hacky and require little bit more work.

Community
  • 1
  • 1
Dhia
  • 10,119
  • 11
  • 58
  • 69