How to perform html markups on site in Django - python

Recently I've started to learn Django, I've decided to make
a sample blog website. I've made Post model which creates and publish post. But there's a problem, I've no idea how to attach html markups to my Post object's attribute for instance "text" e.g
I want to bold my text, but instead text, I see "<b>text</b>".
Here is how I've made Post model:
from django.db import models
from django.utils import timezone
class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
introduction = models.TextField()
text = models.TextField()
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

On your template file use the safe filter like this:
<h1>{{post.title | safe}}</h1>

Related

Copy a model instance and update a filed in new copy

This is my model. I want to make a copy from my model with copy function. and update the created_time to this time and eventually return the post id.
from django.db import models
from django.utils import timezone
class Author(models.Model):
name = models.CharField(max_length=50)
class BlogPost(models.Model):
title = models.CharField(max_length=250)
body = models.TextField()
author = models.ForeignKey(Author, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True)
def copy(self):
blog = BlogPost.objects.get(pk=self.pk)
comments = blog.comment_set.all()
blog.pk = None
blog.save()
for comment in comments:
comment.pk = None
comment.blog_post = blog
comment.save()
return blog.id
class Comment(models.Model):
blog_post = models.ForeignKey(BlogPost, on_delete=models.CASCADE)
text = models.CharField(max_length=500)
I also want copy function makes a copy from post and comments, would you help me to correct my code and update the time in my function.
Intuition
You want to update the date_created of new copied blog post to timezone.now(), instead of date_created of old blog post time, am I right?
I guess the reason of it's not updated, is because when you do blog.pk = None, the blog.date_created is still existed, so even you do blog.save(), blog.date_created is still old value.
Solution
blog.pk = None
blog.date_created = timezone.now() # Update the date_created to the current time
blog.save()

Creating a unique (attribute) title in a Model

I have a blog project and users can create posts with similar titles, How can I prevent a user or even the admin from proceeding without getting an error that the title already exists so that I can avoid future errors in the website such as get() returned more than one Post-it returned 2!
I have tried to use class meta for unique together but still, post was saved with the same title
Here is the post model
class Post(models.Model):
designer = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
slug = models.SlugField(blank=True, null=True, max_length=120)
def __str__(self):
return self.title
class Meta:
unique_together = ('title', 'slug')
You can add unique=True
title = models.CharField(max_length=100, unique=True)
See the docs here.

Add blog posts without category in django 1.11

I want to be able to add some blog posts with categories and some without categories in django. with this models django admin won't let me add blog posts without a category. Thanks.
from django.db import models
from django.db.models import permalink
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
body = models.TextField()
pub_date = models.DateField(db_index=True, auto_now_add=True)
# Many-to-one relationship.
category = models.ForeignKey('blog.Category')
class Category(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
Update your model like this:
category = models.ForeignKey('blog.Category', blank=True, null=True)
blank=True allow forms to have a blank value.
null=True allows a null value in the database.
Edit: here is the documentation

Makemigrations error on model.py

I am new on Python and Django. While trying out an example on a book, I did what the book says and created a models file as follows
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (
('draft','Draft'),
('published','Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='published')
author = models.ForeignKey(User,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices = STATUS_CHOICES,
default='draft')
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
When I run the command, python manage.py makemigrations blog
Its says NameError: name 'STATUS_CHOICES' is not defined
I typed exactly as it is from the book and I am unable to run this command
try to indent your code,you must respect coding style:
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
Change
choices = STATUS_CHOICES,
to
choices = 'STATUS_CHOICES',
You are missing ''.
This is the problem of indent and spacing ,anyways after adding default in publish field and code formatting it worked.
The model.py is hosted here and DONOT edit or open in notepad like editor otherwise it will again create spacing, just import in your IDE

How to delete objects from two apps with the same model name?

I have two apps news and article which both have exactly the same model name Comment:
class Comment(models.Model):
author = models.ForeignKey(User)
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, default='', blank=True)
body = models.TextField()
post = models.ForeignKey(Photo)
published = models.BooleanField(default=True)
Now, in a view I want to delete certain comments from both apps:
Comment.objects.filter(author=someauthor).delete()
How can I achieve that without changing the model names?
You can use import ... as ... so that both model names do not conflict:
from news.models import Comment as NewsComment
from article.models import Comment as ArticleComment
...
NewsComment.objects.filter(author=someauthor).delete()
ArticleComment.objects.filter(author=someauthor).delete()

Categories