I have two models:
class Post(models.Model):
title= models.CharField(max_length=255)
author = models.ForeignKey(User, on_delete=models.CASCADE)
body = models.TextField()
postimage = models.ImageField(null= True, blank= True, upload_to="images/")
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.title + " | "+ str(self.author)
def get_absolute_url(self):
return reverse('article_deatil', args=(str(self.id)))
class AboutMe(models.Model):
title1= models.CharField(max_length=255, default="About Me")
body = models.TextField()
skill1= models.CharField(max_length=255)
skill1body = models.TextField()
skill2= models.CharField(max_length=255)
skill2body = models.TextField()
skill3= models.CharField(max_length=255)
skill3body = models.TextField()
edu1=models.CharField(max_length=255)
edu1body = models.TextField()
edu2=models.CharField(max_length=255)
edu2body = models.TextField()
edu3=models.CharField(max_length=255)
edu3body = models.TextField()
def __str__(self):
return self.title1
I want to show both of them in my home.html
class HomeView(ListView):
model = Post
template_name = 'home.html'
queryset = Post.objects.order_by('-published_date')[:3]
url.py
urlpatterns = [
path('',HomeView.as_view(), name="home"),
path('',PostViewList.as_view(), name="postlist"),
]
I'm new to django and not sure how to show case two model in one template. I did put the post.body and other tags in my html but it not showing the About me part.
Assuming that you want one more queryset of AboutMe model like Post model.
You should simply use get_context_data() in the following way:
class HomeView(ListView):
model = Post
template_name = 'home.html'
queryset = Post.objects.order_by('-published_date')[:3]
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['about_me_records'] = AboutMe.objects.all()
return context
Now, you have one more queryset about_me_records to use in the template and iterate over.
Related
I tried to search for answers, but after a few days, I'm here asking:
I'm a beginner, making a todo list app - expanding on a tutorial I followed. Currently, it's filtering by user, which is fine, but I also want to filter by a field in the DB (list).
Models:
class ToDoList(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
title = models.CharField(max_length=200)
description = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
class Meta:
ordering = ['created']
class Task(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
complete = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
list = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
def __str__(self):
return self.title
class Meta:
ordering = ['complete']
View I'm trying to change:
class TaskList(LoginRequiredMixin, ListView):
model = Task
context_object_name = "tasks"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['tasks'] = context['tasks'].filter(user=self.request.user)
context['count'] = context['tasks'].filter(complete=False).count
search_input = self.request.GET.get('search-area') or ''
if search_input:
context['tasks'] = context['tasks'].filter(title__startswith=search_input)
context['search_input'] = search_input
return context
Also, is there a way to access the list variable in the html component, like here?
url:
path('tasks/<list>/create', TaskCreate.as_view(), name="task-create"),
html:
← Back
i'm building a small webstore , in the product page i put the order form using FormMixin and TemplateView, when i submit the order i get a "Direct assignment to the forward side of a many-to-many set is prohibited. Use product.set() instead." error
Bellow you can check the code
Models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=255, unique=True, )
description = models.TextField(max_length=1500)
class Meta:
verbose_name_plural = "categories"
def __str__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
nominal_price = models.PositiveIntegerField(verbose_name='prix normal',)
reduced_price = models.PositiveIntegerField(blank=True, null=True)
quantity = models.PositiveIntegerField(default=10)
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')
photo = models.ImageField(upload_to="img/products/", default="img/products/user_default.png")
def __str__(self):
return self.name
class Customer(models.Model):
full_name = models.CharField(max_length=150)
address = models.CharField(max_length=1500, null=True)
phone = models.IntegerField()
city = models.CharField(max_length=100)
email = models.EmailField(null=True)
class Order (models.Model):
product = models.ManyToManyField(Product, through='OrderProduct')
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
class OrderProduct(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
Views.py
class ProductDetailView(FormMixin, TemplateView):
model = Product
template_name = 'product.html'
form_class = OrderForm
def get_success_url(self):
return reverse('index')
def post(self, request, *args, **kwargs):
context = self.get_context_data()
form = OrderForm(request.POST)
if context['form'].is_valid():
product = get_object_or_404(Product, name=self.kwargs['product_name'])
customer = form.save()
Order.objects.create(product=product, customer=customer)
return super(TemplateView, self)
def get_context_data(self, **kwargs):
context = super(ProductDetailView, self).get_context_data(**kwargs)
context['product'] = Product.objects.get(name=self.kwargs['product_name'])
context['form'] = self.get_form()
return context
urls.py
path('', views.ProductListView.as_view(), name='index'),
Did i missed something
For handling many-to-many relations, you cannot directly set the product from Order. Also you would need to create the order first before you can set or add a product:
order = Order.objects.create(customer=customer)
order.product.add(product)
I would like to list out the comments on my Post Detail page and wanted to see how I can connect a view to the specific comments for a given post?
Models.py
class Post(models.Model):
owner = models.ForeignKey(
Profile, null=True, blank=True, on_delete=models.SET_NULL)
title = models.CharField(max_length=200)
body = models.TextField()
Post_image = models.ImageField(
null=True, blank=True, default='default.jpeg')
create_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post', kwargs={'pk': self.pk})
class Meta:
ordering = ['-create_date']
class Comment(models.Model):
owner = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
text = models.TextField()
create_date = models.DateTimeField(auto_now_add=True)
Views.py
class PostDetailView(DetailView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['comment_list'] = Post.comment_set.all()
return context
You can access the detail object of the DetailView with self.object:
class PostDetailView(DetailView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['comment_list'] = self.object.comment_set.order_by('create_date')
return context
class Article(models.Model):
Title = models.CharField(max_length = 255)
writing = models.TextField()
category = models.CharField(max_length = 225)
published = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
slug = models.SlugField(blank=True, editable=False)
def save(self):
self.slug = slugify(self.Title)
super().save()
def get_absolute_url(self):
url_slug = {'slug':self.slug}
return reverse('artikel:ArticleDetail', kwargs = url_slug)
def __str__(self):
return "{}.{}".format(self.id, self.Title)
i want to build a simple website using django where it could post some articles by form. The problem is how could i post multiple category in one article ? this is form.py below.
from .models import Article
from django.forms import ModelForm
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = [
'title',
'writing',
'category',
]
You can a ManyToManyField to link your post to multiple categories, for example:
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
def __str__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=255)
writing = models.TextField()
category = models.ManyToManyField(Category)
published = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
slug = models.SlugField(blank=True, editable=False)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super().save()
def get_absolute_url(self):
return reverse('artikel:ArticleDetail', kwargs={'slug': self.slug})
def __str__(self):
return '{}.{}'.format(self.id, self.title)
That being said, there already are some packages for this. django-taggit [GitHub] for example, you can install this with:
pip3 install django-taggit
and include 'taggit' to the INSTALLED_APPS list [Django-doc]:
# settings.py
INSTALLED_APPS = [
# …,
'taggit',
# …
]
Then in your models, you can add a TaggableManager to the model:
from django.db import models
from taggit.managers import TaggableManager
class Article(models.Model):
title = models.CharField(max_length=255)
writing = models.TextField()
categories = TaggableManager()
published = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
slug = models.SlugField(blank=True, editable=False)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super().save()
def get_absolute_url(self):
return reverse('artikel:ArticleDetail', kwargs={'slug': self.slug})
def __str__(self):
return '{}.{}'.format(self.id, self.title)
As the documentation specifies, it comes with a form field that allows one to write space-separated and comma-separated tags.
I have 4 categories on my site. How can I write one class PageViews(ListView) for four same pages with different posts. And same question, how to write class DeteilViews(DetailView) for each posts? My models below.
class Category(models.Model):
category = models.CharField(max_length=50)
slug = models.CharField(max_length=60, unique=True)
class Topic(models.Model):
topic = models.CharField(max_length=50)
slug = models.CharField(max_length=60, unique=True)
category = models.ManyToManyField(Category)
class Page(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
datetime = models.DateTimeField()
title = models.CharField(max_length=60)
slug = models.CharField(max_length=250, unique=True)
short_text = models.CharField(max_length=255)
text = models.TextField()
image = models.ImageField(upload_to='media/news_img')
img_source = models.CharField(max_length=255)
page_source = models.CharField(max_length=60)
parsing_date = models.DateTimeField(auto_now=True)
objects = models.Manager()
def get_absolute_url(self):
return reverse('news:detail', args=[self.category.slug, self.topic.slug, self.slug])
class Comment(models.Model):
page = models.ForeignKey(Page, on_delete=models.CASCADE)
comment = models.TextField()
author = models.CharField(max_length=55)
datetime = models.DateTimeField()
I tried write, but failed...
For example:
class DetailView(DetailView):
template_name = 'post.html'
model = Page
def guery_set(self):
return self.request.GET.get('slug')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Page.objects.filter(slug=slug)
return context
And:
class PageView(ListView, SingleObjectMixin):
template_name = 'page_body.html'
paginate_by = 8
def get(self, request, *args, **kwargs):
self.object = self.get_object(
queryset=Category.objects.all())
return super().get(request, *args, **kwargs)
def get_queryset(self):
return Page.objects.filter(category=self.object)
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
pages = ????
context['posts'] = pages[:8]
return context