Created
January 24, 2016 04:41
-
-
Save bitsandbooks/a29e160777e26d3cd40c to your computer and use it in GitHub Desktop.
Django static and media folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>{{ course.name }}</h1> | |
<p><strong>Description:</strong> {{ course.description }}</p> | |
<p><strong>Student Type:</strong> {{ course.get_student_type_display }}</p> | |
<p><strong>Image</strong> /{{ course.featured_image.url }}<br> | |
<img src="/{{ course.featured_image.url }}"></p> | |
<p><strong>Instructor:</strong> {{ course.instructor }}</p> | |
<p><strong>Number of sessions:</strong> {{ course.number_of_sessions }}</p> | |
<p><strong>Tuition (members):</strong> ${{ course.tuition_members }}</p> | |
<p><strong>Tuition (nonmembers):</strong> ${{ course.tuition_nonmembers }}</p> | |
<p><strong>Registration URL:</strong> <a href="{{ course.registration_url }}">{{ course.registration_url }}</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals | |
from django.db import models | |
import datetime | |
DEFAULT_COURSE_AUDIENCE = ((1, 'Adult'), (2, 'Youth'), (3, 'Both')) | |
DEFAULT_COURSE_ID = 1 | |
DEFAULT_COURSE_IMAGE = 'none' | |
DEFAULT_COURSE_IMAGE_HELP_TEXT = 'Image should be no larger than X px wide by Y px tall.' | |
DEFAULT_COURSE_IMAGE_UPLOAD_TO = '' # Relative to site's main folder. | |
DEFAULT_COURSE_SESSIONS = 4 | |
DEFAULT_COURSE_TIX_URL = 'https://tickets.example.com/' | |
DEFAULT_COURSE_TUITION = 0 | |
DEFAULT_INSTRUCTOR_ID = 1 | |
def course_image_file_name(instance, filename): | |
# This method should return a datestamped, sanitized filename. | |
upload_date = datetime.datetime.utcnow().strftime("%Y%m%d-%H%M%S") | |
return DEFAULT_COURSE_IMAGE_UPLOAD_TO + upload_date + "-" + filename | |
class Instructor(models.Model): | |
name = models.CharField(max_length=200, verbose_name='Name (Last, First)') | |
bio = models.TextField(max_length=1000) | |
visible_on_site = models.BooleanField(default=True) | |
def __str__(self): | |
return self.name | |
class Meta: | |
ordering = ['name'] | |
class Course(models.Model): | |
name = models.CharField(max_length=200) | |
description = models.TextField(max_length=1000) | |
featured_image = models.ImageField(upload_to=course_image_file_name, max_length=100, default=DEFAULT_COURSE_IMAGE, help_text=DEFAULT_COURSE_IMAGE_HELP_TEXT) | |
student_type = models.IntegerField(choices=DEFAULT_COURSE_AUDIENCE, default=1) | |
instructor = models.ForeignKey(Instructor, on_delete=models.CASCADE, default=DEFAULT_INSTRUCTOR_ID) | |
number_of_sessions = models.PositiveIntegerField(default=DEFAULT_COURSE_SESSIONS) | |
tuition_members = models.PositiveSmallIntegerField(verbose_name='Tuition (members)', default=DEFAULT_COURSE_TUITION) | |
tuition_nonmembers = models.PositiveSmallIntegerField(verbose_name='Tuition (nonmembers)', default=DEFAULT_COURSE_TUITION) | |
registration_url = models.URLField(max_length=200, verbose_name='Registration URL', default=DEFAULT_COURSE_TIX_URL) | |
visible_on_site = models.BooleanField(default=False) | |
def __str__(self): | |
return self.name | |
class Meta: | |
ordering = ['name'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import url | |
from . import views | |
urlpatterns = [ | |
url(r'^$', views.index, name='index'), | |
url(r'^adults/$', views.adult_courses, name='adults'), | |
url(r'^youths/$', views.youth_courses, name='youths'), | |
url(r'^instructors/$', views.list_instructors, name='instructors'), | |
url(r'^(?P<course_id>[0-9]+)/$', views.detail, name='detail'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Other stuff above... | |
# Static files (CSS, JavaScript, Images) | |
STATIC_ROOT = os.path.join(BASE_DIR, 'static/') | |
STATIC_URL = 'static/' | |
# Media files (Stuff uploaded by users) | |
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') | |
MEDIA_URL = 'media/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.conf.urls import url, include | |
from django.conf.urls.static import static | |
from django.contrib import admin | |
urlpatterns = [ | |
url(r'^courses/', include('courses.urls')), | |
url(r'^admin/', admin.site.urls), | |
] | |
if settings.DEBUG is True: | |
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment