Oops!
I did it again!
By Curtis Maloney
Not so obvious mistakes we all make.
from django.views.generic import ListView
from .models import Post
def index(request):
return render(request, 'blog/index.html')
class PostMixin(object):
model = Post
def get_queryset(self):
return super().get_queryset().active().order_by('-post_date')
class PostListView(ListView, PostMixin):
template_name = 'blog/post_list.html',
def get_context_data(self, **kwargs):
data = super().get_context_data(self, **kwargs)
data['tags'] = Tag.objects.filter(post__in=self.object_list)
return data
class PostDetailView(DetailView, PostMixin):
def get_context_data(self, **kwargs):
data = super(DetailView, self).get_context_data(**kwargs)
data['tags'] = Tag.objects.current()
return data
blog/views.py
accounts/views.py
from django.shortcuts import render, redirect
from .models import Tag
tag_count = Tag.objects.count()
def index(request):
if not request.user.is_authenticated:
return redirect('login')
return render(request, 'accounts/index.html')
def add_tags(request, default_tags=[]):
'''
Show a list of tags to add to your account
'''
tags = default_tags
tags += [ t.name for t in Tag.objects.default() ]
return render(request, 'accounts/add_tags.html', {
'tags': tags,
'tag_count': tag_count,
})
from django.conf.urls import url
from django.shortcuts import render
from accounts import views
from blog import views
urlpatterns = [
url(r'^$', render, {'template_name': 'index.html'}, name='home'),
url(r'^profile/', views.index, name='profile'),
url(r'^profile/edit/', views.edit, name='profile-edit'),
url(r'^profile/tag/', views.add_tags, {'default_tags': ['human']}, name='profile-tags'),
url(r'^blog/', views.blog_list, name='blog-list'),
url(r'^blog/(?P<slug>[-\w])/$', views.blog_detail, name='blog-detail'),
url(r'^blog/(?P<slug>[-\w]*/comment/$', views.blog_comment, name='blog-comment'),
]
urls.py
settings.py
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static/'))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')
Oops!
By Curtis Maloney
Oops!
- 1,357