【问题标题】:context_processor with if inside带有 if 的 context_processor
【发布时间】:2020-10-27 14:30:54
【问题描述】:

首先, 感谢所有让我在 django 上变得更好的答案,我还不是“超级”djanglers,但我正在前进。 我会遇到一个项目的问题。 当我进入 django 时,我会一直遇到同样的问题: 我安装了 django allauth,以便在马俱乐部注册几个字段 在这个字段中,我将有一个带有挂载级别的选择字段。 这是模型代码:

from django.db import models
from django.db import models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
    PRIME = (
        ('régulier', 'régulier'),
        ('occasionnel', 'occasionnel'),
        ('concours', 'concours'),
    )

    NIVEAUX = (
        ('débutant', 'débutant'),
        ('galop1', 'Galop 1'),
        ('galop2', 'Galop 2'),
        ('galop3', 'Galop 3'),
        ('galop4', 'Galop 4'),
        ('galop5', 'Galop 5'),
        ('galop6', 'Galop 6'),
        ('galop7', 'Galop 7'),
    )

    AGE = (
        ('18 mois - 3ans', '18mois - 3ans'),
        ('3 - 5 ans', '3 - 5 ans'),
        ('6 - 12 ans', '6 - 12 ans'),
        ('13 - 60 ans', '13 - 60 ans'),
    )

    PHOTO =(
        ('oui', 'oui'),
        ('non', 'non'),
    )
    prenom = models.CharField(max_length= 120)
    nom = models.CharField(max_length= 120)
    telephone = models.CharField(max_length=12)
    adresse = models.CharField(max_length=250)
    ville = models.CharField(max_length=120)
    code_postal = models.IntegerField(default="63120")
    occurence = models.CharField(choices=PRIME ,max_length = 150, default='régulier')
    niveaux = models.CharField(choices=NIVEAUX ,max_length = 150, default='débutant', help_text="Quel niveau validé avez vous obtenus ? ")
    age = models.CharField(choices=AGE ,max_length = 150, default='18 mois - 3ans', help_text=" A quelle tranche d'age allez vous appartenir ?")
    photo = models.CharField(choices=PHOTO ,max_length = 150, default='oui', help_text=" Acceptez vous d'être photographié ?")

我会有一个博客,会向所有人和特定级别(“niveaux”)内的人显示帖子 我将遇到的实际问题是将视图和 this 放到所有页面中,所以我将考虑使用 context_processor 我已经尝试将其作为上下文处理器:

from .models import News
from accounts.models import CustomUser
from django.conf import settings

def galopage(request):
    #creer les variables de groupes
    group1 = CustomUser.objects.filter(niveaux='débutant')
    group2 = CustomUser.objects.filter(niveaux='galop1')
    group3 = CustomUser.objects.filter(niveaux='galop2')
    group4 = CustomUser.objects.filter(niveaux='galop3')
    group5 = CustomUser.objects.filter(niveaux='galop4')
    group6 = CustomUser.objects.filter(niveaux='galop5')
    group7 = CustomUser.objects.filter(niveaux='galop6')
    group8 = CustomUser.objects.filter(niveaux='galop7')

    if group1 == True:
        niveaux = News.objects.filter(choix='débutant')
        return {'retourgroup': niveaux}

    elif group2 == True:
        niveaux = News.objects.filter(choix='galop1')
        return {'retourgroup': niveaux}

    elif group3 == True:
        niveaux = News.objects.filter(choix='galop2')
        return {'retourgroup': niveaux}

    elif group4 == True:
        niveaux = News.objects.filter(choix='galop3')
        return {'retourgroup': niveaux}

    elif group5 == True:
        niveaux = News.objects.filter(choix='galop4')
        return {'retourgroup': niveaux}

    elif group6 == True:
        niveaux = News.objects.filter(choix='galop5')
        return {'retourgroup': niveaux}

    elif group7 == True:
        niveaux = News.objects.filter(choix='galop6')
        return {'retourgroup': niveaux}

    elif group8 == True:
        niveaux = News.objects.filter(choix='galop7')
        return {'retourgroup': niveaux}

    else:
        pass

我会把这个作为答案: 模块“News.context_processors”没有定义“请求”属性/类

博客部分:

    from django.db import models

class News(models.Model):
    NIVEAUX = (
        ('visiteur', 'visiteur'),
        ('débutant', 'débutant'),
        ('galop1', 'galop1'),
        ('galop2', 'galop2'),
        ('galop3', 'galop3'),
        ('galop4', 'galop4'),
        ('galop5', 'galop5'),
        ('galop6', 'galop6'),
        ('galop7', 'galop7'),
    )
    title = models.CharField(max_length=150)
    content = models.TextField()
    choix = models.CharField(choices=NIVEAUX, max_length=15, default='visiteur')
    
 
    def __str__(self):
        return self.title

(我将在上下文处理器中添加“News.context_processors.request”)

这是我在几个项目中会遇到大量时间的问题, 我无法为此找到解决方案,我会对此感到非常愚蠢。

感谢您的时间和考虑, 尼古拉斯

【问题讨论】:

    标签: django view model choicefield


    【解决方案1】:

    如果 News 是您的应用名称(基本上是“app_name.context_processors.function_name”),那么您应该添加

    "News.context_processors.galopage"
    

    进入位于 settings.py 模板下的“context_processors”

    【讨论】:

    • 好吧,我会尝试,但它不起作用,可悲的是我不确定实现我需要的最佳方法,我需要选择显示来自博客的帖子例如“galop1”给用户在表字段中使用“galop1”......有什么想法会更容易吗?
    猜你喜欢
    • 2012-07-11
    • 2011-09-23
    • 1970-01-01
    • 2015-02-07
    • 2012-01-27
    • 2016-08-15
    • 2014-10-26
    • 2016-01-23
    • 2012-12-18
    相关资源
    最近更新 更多