csrf_exempt for class based views

class ErrorReportView(View): def get(self, request, *args, **kwargs): return HttpResponse(‘Hello, World!’) @method_decorator(csrf_exempt) def post(self, request, *args, **kwargs): return HttpResponse(‘Hello, World!’) I use Postman, but I get <p>CSRF verification failed. Request aborted.</p> Documentation: https://docs.djangoproject.com/en/4.1/topics/class-based-views/intro/#decorating-the-class Could you help me? >Solution : The csrf_exempt decorator should be applied to "dispatch" method. You can add the following decorator directly on… Read More csrf_exempt for class based views

Django query __startswith is not case sensitive

I have been testing user searching with sorted results and I found this strange behavior >>> User.objects.filter(username__istartswith="AbC") <QuerySet [<User: AbC>, <User: AbCuuu>, <User: abc>, <User: abcuuu>]> >>> User.objects.filter(username__startswith="AbC") <QuerySet [<User: AbC>, <User: AbCuuu>, <User: abc>, <User: abcuuu>]> Shouldn’t __startswith only have 2 of those results? I need to actually search with case sensitivity, how do… Read More Django query __startswith is not case sensitive

Error when using comment form on website: (1048, "Column 'comment_post_id' cannot be null")

I’m trying to implement a comment section below each blog on my site. I’ve got the form rendering but when I try to post a comment I get the following error: (1048, "Column ‘comment_post_id’ cannot be null") I cannot see what I’m doing wrong, I’ve also followed a tutorial step by step, although it is… Read More Error when using comment form on website: (1048, "Column 'comment_post_id' cannot be null")

Need help in Python Django for fetch the records from DB which will expire in 30 days

I wrote an application where there is a requirment to display only those records which will expire is next 30 day. models.py class SarnarLogs(models.Model): request_type_choice = ( (‘NAR’, ‘NAR’), ) source = models.CharField(max_length=100) expiry_date = models.DateField() def __str__(self): return self.source Views.py @login_required(login_url=(‘/login’)) def expiry_requests(request): Thirty_day = end_date = datetime.now().date() + timedelta(days=30) posts = SarnarLogs.expiry_date.filter(expiry_date =… Read More Need help in Python Django for fetch the records from DB which will expire in 30 days

Django – How to use delete() in ManyToMany relationships to only delete a single relationship

I have a model Voucher that can be allocated to several users. I used a M2M relationship for it. I want, in the template, the possibility to delete the voucher allocated to the logged in user, and the logged in user only (not all relationships). The problem I have is that the current model deletes… Read More Django – How to use delete() in ManyToMany relationships to only delete a single relationship

how to set query for show followed posts in home page

I want to make query to show all followed posts in the main page, could you help me in doing this? Here’s my file models.py: class Relation(models.Model): from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name=’follower’) to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name=’following’) created = models.DateTimeField(auto_now_add=True) class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, related_name=’profile’) avatar = models.FileField(default=’default.jpg’, verbose_name=’avatar’) age = models.PositiveSmallIntegerField(default=0) location… Read More how to set query for show followed posts in home page

How to fetch a boolean value from models for a specific user in Django?

I have an extended user model and I want to check it to see if the logged-in user has completed_application as per the model: Models.py: class Completed(models.Model): class Meta: verbose_name_plural = ‘Completed Application’ user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) completed_application = models.BooleanField(default=False) def __str__(self): return f'{self.completed_application}’ Views.py: @login_required def dashboard(request): if Completed.objects.get(completed_application = True): completed = True… Read More How to fetch a boolean value from models for a specific user in Django?

Django: Filtering items in generic.ListView

I’m creating a game website and i have these models for games: class Game(models.Model): accountant = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name=’games’) title = models.CharField(max_length=50) bank_money = models.IntegerField() player_starting_money = models.IntegerField() golden_card_amount = models.PositiveIntegerField( validators=[ MinValueValidator(100), MaxValueValidator(1000) ] ) now i want every user to see their own games at dashbard: class DashboardView(mixins.LoginRequiredMixin, generic.ListView): template_name = ‘games/dashboard.html’ model… Read More Django: Filtering items in generic.ListView