简体   繁体   中英

context_processor with if inside

first of all, thanks for all the answers that have permit me to get better at django, i'm not yet an "uber" djanglers but i'm moving forward. I'll got a problem with a project. As i'll move into django i'll keep being stuck with the same problem: I got django allauth installed to have several field in registration for a horse club in this fields i'll have a choices field, with the mount level. here is the model code:

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é ?")

and i'll got a blog that will show post to everyone and to people inside a specific level ("niveaux") the actual problem i'll have is to make the view that will and this into all page, so i'll go to the idea of a context_processor I have tried this as 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

and i'll have this as answer: Module "News.context_processors" does not define a "request" attribute/class

the blog part:

    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

(i'll added 'News.context_processors.request', to context processors)

This is a problem i'll encounter a large number of time in several project, I can't get a solution for this and i'll feel really dumb about it.

Thanks for your time and consideration, Nicolas

if News is your app name (basically "app_name.context_processors.function_name"), then you should add

"News.context_processors.galopage"

into your 'context_processors' which is under TEMPLATES of settings.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM