Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I have a model called Post. The data in Post is displayed to every user while I want this to be user specific.
I'm new to Django. This app is created following the Django for Girls tutorial. I later added support for user registration.
models.py:
from django.conf import settings from django.db import models from django.utils import timezone class Post(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) title = models.CharField(max_length=200) 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
Example from views.py:
from django.shortcuts import render, get_object_or_404 from django.utils import timezone from .models import Post from .forms import PostForm from django.contrib.auth.forms import UserCreationForm from django.shortcuts import redirect from django.contrib.auth.decorators import login_required from django.contrib.auth import login, authenticate @login_required def post_list(request): posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') return render(request, 'blog/post_list.html', {'posts': posts})
Example from the template (post_list.html):
{% for post in posts %} <div class="post"> <div class REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
Change your filter to filter by user as well as published_date:
posts = Post.objects.filter( published_date__lte=timezone.now(), author=request.user ).order_by('published_date')
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.