template not displaying information - django - python

I can't figure out why my data won't display in my user.html template. From reviewing the code it looks like everything is linked correctly. The users.html page is showing up on the website minus the information about {{ user.order_set.all }}. I have highlighted the code I believe relates to the issue. Any guidance would be greatly appreciated, thanks!
user.html - html for displaying the data
{% extends "fuisce/base.html" %}
{% block content %}
**{{ user.order_set.all }}**
{% endblock content %}
views.py - order
from django.shortcuts import render, HttpResponseRedirect
from django.urls import reverse
# Create your views here.
from carts.models import Cart
from .models import Order
from users.models import Profile
from .utils import id_generator
**def orders(request):
context = {}
template = "orders/user.html"
return render(request, template, context)**
def checkout(request):
try:
the_id = request.session['cart_id']
cart = Cart.objects.get(id=the_id)
except:
the_id = None
return HttpResponseRedirect(reverse("cart"))
new_order, created = Order.objects.get_or_create(cart=cart)
if created:
new_order.order_id = id_generator()
new_order.save()
my_p = Profile.objects.get(user=request.user)
new_order.user = my_p
new_order.save()
if new_order.status == "Finished":
# cart.delete()
del request.session['cart_id']
del request.session['items_total']
return HttpResponseRedirect(reverse("cart"))
context = {}
template = "fuisce/about.html"
return render(request, template, context)
models.py - orders
from django.db import models
# Create your models here.
from carts.models import Cart
from users.models import Profile
STATUS_CHOICES =(
("Started", "Started"),
("Abandoned", "Abandoned"),
("Finished", "Finished"),
)
class Order(models.Model):
**user = models.ForeignKey('users.Profile', null=True, blank=True, on_delete=models.CASCADE)**
order_id = models.CharField(max_length=120, default='ABC', unique=True)
cart = models.ForeignKey('carts.Cart', on_delete=models.CASCADE)
status = models.CharField(max_length=120, choices=STATUS_CHOICES, default="Started")
sub_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
tax_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
final_total = models.DecimalField(default=10.99, max_digits=1000, decimal_places=2)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __string__(self):
return self.order_id
urls.py
from django.urls import path
from . import views
from carts import views as cart_views
from orders import views as order_views
urlpatterns = [
path('', views.home, name='fuisce-home'),
path('subscription/', views.subscription, name='fuisce-subscription'),
path('oneoff/', views.oneoff, name='fuisce-oneoff'),
path('about/', views.about, name='fuisce-about'),
path('contact/', views.contact, name='fuisce-contact'),
path('cart/', cart_views.view, name='cart'),
path('cart/<int:id>/', cart_views.remove_from_cart, name='remove_from_cart'),
path('cart/<slug>/', cart_views.add_to_cart, name='add_to_cart'),
path('checkout/', order_views.checkout, name='checkout'),
**path('orders/', order_views.orders, name='user_orders'),**
]

Based on the comments to the question, it seems you have a ForeignKey field to Profile and not to User.
Either change your ForeignKey from Profile to User like this:
class Order(models.Model):
user = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
null=True,
blank=True)
Or use a different lookup in your template:
{{ user.profile.order_set.all }}

Related

models.py order of the models gives NameError: name 'Category/Post' is not defined

I'm new to Django so this is probably a dumb question but,
when I put the class Category model above the class Post model I get an
NameError: name 'Post' is not defined error.
but when I try to put class Category model underneath the Post model (as in the code here) I get
categories = models.ManyToManyField(Category)
NameError: name 'Category' is not defined error.
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE) #if is deleted than delete their posts
location = models.CharField(max_length=100, default="")
tags = TaggableManager()
likes = models.ManyToManyField(User, related_name='blog_posts')
categories = models.ManyToManyField(Category)
def total_likes(self):
return self.likes.count()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Category(models.Model):
post = models.ForeignKey(Post, related_name="categories")
name = models.CharField(max_length=20)
def __str__(self):
return self.name
admin.py
from django.contrib import admin
from .models import Post, Comment, Category #, Konum
# Register your models here.
admin.site.register(Post)
admin.site.register(Comment)
admin.site.register(Category)
#admin.site.register(Konum)
some of the code
<form method="GET" action=".">
<div class="form-group col-md-4">
<label for="category">Category</label>
<select id="category" class="form-control" name="category">
<option selected>Choose...</option>
{% for cat in categories %}
<option value="{{ cat }}">{{ cat }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
views.py
def home(request):
context = {
"posts": Post.objects.all()
}
return render(request, 'blog/home.html', context)
#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------#--------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------
def filter(request):
qs = Post.objects.all()
categories = Category.objects.all()
id_exact_query = request.GET.get('id_exact')
title_or_author_query = request.GET.get('title_or_author')
category = request.GET.get('category')
if is_valid_queryparam(category) and category != 'Choose...':
qs = qs.filter(categories__name=category)
context = {
'posts': qs,
'categories': Category.objects.all()
}
return render(request, 'blog/home.html', context)
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 199
class UserPostListView(ListView):
model = Post
template_name = 'blog/user_posts.html'
context_object_name = 'posts'
paginate_by = 199
def get_queryset(self):
return Post.objects.filter(author = user).order_by('-date_posted')
urls.py
from django.urls import path, re_path
from .import views
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeleteView, UserPostListView, TagIndexView, LikeView #, LocationPostListView
urlpatterns = [
path('', PostListView.as_view(), name="blog-home"), #has a empty strting bc its already processed blog part in main urls
path('user/<str:username>', UserPostListView.as_view(), name="user-posts"),
#--------------------------------------------------------------------------------------------------------------------------------------
#path('location/<str:loc>', LocationPostListView.as_view(), name="location-posts"),
#--------------------------------------------------------------------------------------------------------------------------------------
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),#pk means primary key like post 1 post 2 etc
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('about/', views.about, name="blog-about"),
#--------------------------------------------------------------------------------------------------------------------------------------
path('tag/<slug:slug>/', TagIndexView.as_view(), name='tagged'),
path('like/<int:pk>', LikeView, name='like_post'),
#--------------------------------------------------------------------------------------------------------------------------------------
]
You can use a string literal to specify the model name of a model that still needs to be defined, so you can use ManyToManyField('Category') or ForeignKey('Post', on_delete=models.CASCADE) for example to refer to models not yet defined:
from django.conf import settings
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
location = models.CharField(max_length=100, default="")
tags = TaggableManager()
likes = models.ManyToManyField(settings.AUTH_USER_MODELS, related_name='liked_posts')
categories = models.ManyToManyField('Category')
It however does not seem to make much sense that a Category has a ForeignKey to a post: that would mean that a Category links to exactly one Post record?
You can for example use a ListView with:
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 199
def get_queryset(self):
qs = super().get_queryset()
if self.request.GET.get('category'):
return qs.filter(categories__name=self.request.GET['category'])
return qs
def get_context_data(self, *args, **kwargs):
context = super().get_queryset(*args, **kwargs)
context['categories'] = Category.objects.all()
return context
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

how to query get posts filtered with same category in django functional based view

I am trying to list out all posts with same catgory on my home page in django. I want title to be category title and then below title it should render all the posts related to that catgory. I did this in class based view by finding help on stackoverflow but I want to know this in functional based view to understand this.
posts/models.py
from tinymce import HTMLField
from django.db import models
from django.contrib.auth import get_user_model
from slugger import AutoSlugField
from django.urls import reverse
# Create your models here.
User = get_user_model()
def upload_location(instance, filename):
return "%s/%s" %(instance.slug, filename)
class Author(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
class Category(models.Model):
title = models.CharField(max_length=20)
slug = AutoSlugField(populate_from='title')
def __str__(self):
return self.title
class Post(models.Model):
title = models.CharField(max_length = 100)
slug = AutoSlugField(populate_from='title')
overview = models.CharField(max_length= 200)
timestamp = models.DateTimeField(auto_now_add=True)
content = HTMLField()
comment_count = models.IntegerField(default=0)
view_count = models.IntegerField(default=0)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
thumbnail = models.ImageField(
upload_to=upload_location,
null=True,
blank=True)
category = models.ManyToManyField(Category)
featured = models.BooleanField()
previous_post = models.ForeignKey('self', related_name= 'previous', on_delete=models.SET_NULL, blank=True, null=True)
next_post = models.ForeignKey('self', related_name= 'next', on_delete=models.SET_NULL, blank=True, null=True)
def __str__(self):
return self.title
posts/views.py I tried to do this but this gives me error something like this
TypeError at /
index() missing 1 required positional argument: 'category_slug'
from django.shortcuts import render
from .models import Post, Author, Category
# Create your views here.
def index(request):
featured = Post.objects.filter(featured = True) #put this on carousel
latest_post = Post.objects.order_by('-timestamp')[:6]
category = Category.objects.filter(slug= category_slug)
post_by_category = Post.objects.filter(category=category)
context = {
'object_list': featured,
'latest_post': latest_post,
'post_by_category': post_by_category,
}
return render(request, 'index.html', context)
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from posts.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
path('tinymce/', include('tinymce.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
when you use a function based view like yours: def index(request, category_slug): you are declaring a query_param and you have to catch it in your url. Try this:
path('<str:category_slug>', index)
as your indexview
So this will be your url where you get the category slug to filter out the posts
path('<str:category_slug>', index)
Your request function must have the url parameter as an argument so that you can query the category from the db, in this case it is category_slug
def index(request, category_slug):
category = Category.objects.get(slug=category_slug)
post_by_category = Post.objects.filter(category=category)
Remember, filter() returns a list of instances, so use get() instead.

Unable to use data of one app in another in Django

I have three apps (Internship, UserProfile and Infrastructure) in my django project. I have made models Profile and StudentProject in UserProfile Model. The StudentProject Model contains two foreign key-> user and Lab (this model is defined in the Infrastructure model). In a template(details.html file) in Infrastructure app, i want to retrieve all StudentProjects who have their foreign key as the lab whose details are currently being shown. I am unable to bring the student projects created here. Please help someone. I have already tried to use filter but it doesn't work!
userprofile/models.py file
from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse
from infrastructure.models import Lab
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
type = models.CharField(max_length=100)
profile_picture = models.FileField()
skills = models.CharField(max_length=1000)
def get_absolute_url(self):
return reverse('userprofile:index')
def __str__(self):
return self.name
class StudentProject(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
project_picture = models.FileField()
lab = models.ForeignKey(Lab)
mentor = models.CharField(max_length=100)
domain = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('userprofile:index')
infrastructure/models.py file
from django.db import models
from django.core.urlresolvers import reverse
class Lab(models.Model):
name = models.CharField(max_length=100)
department = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
lab_logo = models.FileField()
def get_absolute_url(self):
return reverse('infrastructure:details', kwargs={'pk': self.pk})
def __str__(self):
return self.name
infrastructure/templates/details.html
->This is where i want all student projects of lab to be shown
{% extends 'infrastructure/base.html' %}
{% block body %}
<h1>This is details page</h1>
<img src="{{ lab.lab_logo.url }}" style="width: 300px;">
<h1>{{ lab.name }}</h1>
<h2>{{lab.projects}}</h2>
{% endblock %}
infrastructure/views.py file
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from infrastructure.models import Lab
from django.views.generic.edit import CreateView, UpdateView, DeleteView
class IndexView(generic.ListView):
template_name = 'infrastructure/index.html'
context_object_name = 'all_labs'
def get_queryset(self):
return Lab.objects.all()
class DetailView(generic.DetailView):
model = Lab
template_name = 'infrastructure/details.html'
class LabCreate(generic.CreateView):
model = Lab
fields = ['name', 'department', 'description', 'lab_logo']
class LabUpdate(generic.UpdateView):
model = Lab
fields = ['name', 'department', 'description', 'lab_logo']
class LabDelete(DeleteView):
model = Lab
success_url = reverse_lazy('infrastructure:index')
infrastructure/urls.py file
from django.conf.urls import url
from . import views
app_name = 'infrastructure'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='details'),
url(r'^lab/add/$', views.LabCreate.as_view(), name='lab-add'),
url(r'^lab/(?P<pk>[0-9]+)/$', views.LabUpdate.as_view(), name='lab-update'),
url(r'^lab/(?P<pk>[0-9]+)/delete/$', views.LabDelete.as_view(), name='lab-delete'),
]
userprofile/views.py file
from django.contrib.auth import login, authenticate, logout
from django.http import request
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from .models import Profile, StudentProject
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import UserForm
from django.views import generic
from django.views.generic import View
class IndexView(generic.TemplateView):
template_name = 'userprofile/index.html'
context_object_name = 'user_profile'
def get_queryset(self):
return request.user.get_profile()
class ProfileCreate(CreateView):
model = Profile
fields = ['user', 'name', 'type', 'profile_picture', 'skills']
class ProfileUpdate(UpdateView):
model = Profile
fields = ['user', 'name', 'type', 'profile_picture', 'skills']
class StudentProjectCreate(CreateView):
model = StudentProject
fields = ['user', 'title', 'project_picture', 'lab', 'mentor', 'domain', 'description']
class StudentProjectUpdate(UpdateView):
model = StudentProject
fields = ['user', 'title', 'project_picture', 'lab', 'mentor', 'domain', 'description']
class StudentProjectDelete(DeleteView):
model = StudentProject
success_url = reverse_lazy('userprofile:index')
def logout_view(request):
logout(request)
return render(request, 'userprofile/logout.html')
# for new user
class UserFormView(View):
form_class = UserForm
template_name= 'internship/registration_form.html'
# display blank form
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
# process from data
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned (normalized) data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
# return User object if credentials are correct
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('userprofile:profile-add')
return render(request, self.template_name, {'form': form})
userprofile/urls.py file
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
from userprofile import views
app_name = 'userprofile'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^profile/add/$', views.ProfileCreate.as_view(), name='profile-add'),
url(r'^profile/(?P<pk>[0-9]+)/$', views.ProfileUpdate.as_view(), name='profile-update'),
url(r'^logout$', views.logout_view, name='logout_view'),
url(r'^login/$', auth_views.login, {'template_name': 'userprofile/login.html'}),
url(r'^register/$', views.UserFormView.as_view(), name='register'),
url(r'^studentproject/add/$', views.StudentProjectCreate.as_view(), name='student-project-add'),
url(r'^studentproject/(?P<pk>[0-9]+)/$', views.StudentProjectUpdate.as_view(), name='student-project-update'),
url(r'^studentproject/(?P<pk>[0-9]+)/delete/$', views.StudentProjectDelete.as_view(), name='student-project-delete'),
]
Basically, you need to add related_name argument to your declaration of Lab Field in StudentProject Model. That will make children accessible from parent Model further:
class StudentProject(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
project_picture = models.FileField()
lab = models.ForeignKey(Lab, related_name=projects)
...
And now, in your template, you can easily iterate over your current lab's projects:
{% extends 'infrastructure/base.html' %}
{% block body %}
<h1>This is details page</h1>
<img src="{{ lab.lab_logo.url }}" style="width: 300px;">
<h1>{{ lab.name }}</h1>
{% for project in lab.projects_set.all %}
<h2>{{project.title}}</h2>
{% endfor %}
{% endblock %}

How to display Model data in a Django Template

I am new to Django. I am trying to display data from my Project model in my index view, using a template. I tried my best to structure this app similar to the polls app. I'm not sure what I am doing wrong. I am using python 2.7, and django 1.8.6
Here is my url:
from django.conf.urls import url
from . import views
app_name = 'project'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
Here is my Model:
import datetime
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.contrib.auth.models import User
from django.utils import timezone
#python_2_unicode_compatible # only if you need to support Python 2
class Contractor(models.Model):
#project
name = models.CharField(max_length=50)
address = models.CharField(max_length=100, blank=True)
phone = models.CharField(max_length=14, blank=True)
city = models.CharField(max_length=60, blank=True)
state = models.CharField(max_length=2, blank=True)
created_by = models.ForeignKey(User, related_name='Contractor_created_by')
created_date = models.DateTimeField()
modified_by = models.ForeignKey(User, related_name='Contractor_modified_by')
modified_date = models.DateTimeField()
def __str__(self):
return self.name
#python_2_unicode_compatible # only if you need to support Python 2
class Project(models.Model):
name = models.CharField(max_length=50)
jobNumber = models.CharField(max_length=8)
shopOut = models.DateTimeField(null=True)
shopIn = models.DateTimeField(null=True)
delivery = models.DateTimeField(null=True)
job1 = models.CharField(max_length=50, null=True)
job2 = models.CharField(max_length=50, null=True)
job3 = models.CharField(max_length=50, null=True)
contractor = models.ForeignKey(Contractor, on_delete=models.CASCADE, default=101)
created_by = models.ForeignKey(User, related_name='Project_created_by')
created_date = models.DateTimeField(auto_now_add=True)
modified_by = models.ForeignKey(User, related_name='Project_modified_by')
modified_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.id:
self.created_by = User.objects.get(id=1)
self.modified_by = User.objects.get(id=1)
super(Project, self).save(*args, **kwargs)
year = datetime.datetime.now().year
self.jobNumber = '{}{:04d}'.format(year, self.id)
self.modified_by = User.objects.get(id=1)
super(Project, self).save(*args, **kwargs)
Here is my View:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from django.utils import timezone
from .models import Project
# Create your views here.
class IndexView(generic.ListView):
model = Project
template_name = 'project/index.html'
def get_queryset(self):
return Project.objects
class DetailView(generic.DetailView):
model = Project
Here is my Template:
{% load staticfiles %}
<h1>Projects</h1>
<ul>
{% for projects in project.get_queryset %}
in for loop
<!-- <li>{{ projects.name }}</li> -->
<li>Test</li>
{% endfor %}
</ul>
end of list
When I go to the page I get a h1 Project, an empty ul, and a line that says 'end of list'
In your get_queryset, you should return Project.objects.all().
In your template, you don't need to do project.get_queryset, the get_queryset method is called for you and the values are passed to the template as object_list and <objectname>_list, along with other parameters. In your case, the object is Project so there should be a project_list variable too along with object_list.
You can do:
{% for project in project_list %}
<li>{{ project.name }}</li>
{% endfor %}
Or:
{% for project in object_list %}
<li>{{ project.name }}</li>
{% endfor %}
You can read more about it here: https://docs.djangoproject.com/en/1.9/ref/class-based-views/generic-display/#listview
Your get queryset doesn't return a query set at the minute, currently its just returning a related manager. you should make it return a queryset...
def get_queryset(self):
return Project.objects.all() # added all
You might want to try:
{% for project in object_list %}
<li>{{ project.name }}</li>
{% endfor %}
object_list is the default name of queryset if you use ListView, you can change that by defining context_object_name in your view. Here's django doc about that.

Problems with rendering page on Django framework

I have categories of the news and news and i don't know how to render the page with the list of news that belong to the same category. Hope You will help me with that.
this is my model.py file:
from django.db import models
class Category(models.Model):
class Meta:
verbose_name_plural = u'Categories'
category = models.CharField(max_length=255)
slug = models.CharField(max_length=255)
def __unicode__(self):
return self.category
class News(models.Model):
class Meta:
verbose_name_plural = u'News'
title = models.CharField(max_length=255)
category = models.ForeignKey(u'Category', related_name=u'Category', blank=True, null=True)
pub_date = models.DateTimeField('date published')
slug = models.CharField(max_length=255)
short_content=models.TextField(max_length=2000, blank=True)
content = models.TextField(max_length=10000)
image = models.FileField(u'Images', upload_to='media/img/news', blank=True)
def __unicode__(self):
return self.title
views.py:
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from news.models import News
class NewsListView(ListView):
template_name ='news/list.html'
context_object_name = 'news_list'
def get_queryset(self):
return News.objects.order_by('-pub_date') [:5]
class NewsDetailView(DetailView):
model=News
template_name = 'news/detail.html'
def CategoryNews(request, categoryslug):
category = Category.objects.get(slug=categoryslug)
news = News.objects.filter(category=category)
return render (request, 'news/category_news.html', {'category' : category})
urls.py:
django.conf.urls import patterns, url
from news.views import NewsListView, NewsDetailView
from news import views
urlpatterns = patterns('',
url(r'^$', NewsListView.as_view(), name='list'),
url(r'^(?P<slug>[-_\w]+)/$', NewsDetailView.as_view()),
url(r'^kids-garden/$', views.CategoryNews),
)
Thank You!
On CategoryNews view add news to render context. This will make news items available in templates.
def CategoryNews(request, categoryslug):
category = Category.objects.get(slug=categoryslug)
news = News.objects.filter(category=category)
return render (request, 'news/category_news.html', {'category' : category, 'newsitems': news})
Add named group to category url to make it dynamic.
Rewrite
url(r'^kids-garden/$', views.CategoryNews, name='category'),
to
url(r'^category/(?P<categoryslug>\w+)/$', views.CategoryNews, name='category'),
In category_news.html
{%for news in newsitems%}
{{ news.title }}
{{ news.content }}
# rest values...........
{% endfor %}
I think this should do the job:
views.py
....
from news.models import News, Category
def CategoryNews(request, categoryslug):
category_news = News.objects.filter(category__slug='categoryslug')
return render (request, 'news/category_news.html', {'category_news' : category_news})

Categories

Resources