首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内部带有if的context_processor

内部带有if的context_processor
EN

Stack Overflow用户
提问于 2020-07-07 18:43:21
回答 1查看 21关注 0票数 0

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

代码语言:javascript
复制
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")我将遇到的实际问题是将视图和这些内容放到所有页面中,所以我将使用context_processor的想法我已经尝试过将其作为上下文处理器:

代码语言:javascript
复制
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“没有定义"request”属性/类

博客部分:

代码语言:javascript
复制
    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‘添加到上下文处理器中)

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

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

EN

回答 1

Stack Overflow用户

发布于 2020-07-07 19:06:06

如果新闻是你的应用程序名称(基本上是"app_name.context_processors.function_name"),那么你应该添加

代码语言:javascript
复制
"News.context_processors.galopage"

到settings.py模板下的'context_processors‘中

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62773460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档