em four
em four

Reputation: 361

how to to display the category user created by the current user on every page

as title indicates I'm trying to send the name of a category user created by the current user on every page. My initial attempt was simply

{% if user.is_authenticated == category.author %}
  {{category.name}}
 {% endif %}

but this only displays the category in a certain page, while I want to display this in the navbar which I have it included for the every page. So I thought I should do category = models.foreignkey('category') in my user model but got told I should set a queryset in a template context processor. which I'm not sure it's the best way to do. Can someone please direct me how I should do such matter?

here's my code

class Category(models.Model): 
        name = models.CharField(max_length=128, unique=True)
        description = models.TextField()
        author = models.ForeignKey(settings.AUTH_USER_MODEL)

and in my views.py

@login_required
def add_category(request):
    if not request.user.is_superuser and Category.objects.filter(author=request.user).exists():
        return render(request,'main/category_already_exists.html')
    if request.method == 'POST':
        category = Category(author=request.user)
        form = CategoryForm(request.POST, request.FILES, instance=category)
        if form.is_valid():
            form.save(commit=True)
            return redirect('category', category_name_url=category.name)

    else:
        form = CategoryForm()
    context = {
        "form":form
    }
    return render(request, 'main/add_category.html',context)

and this is my simplified category view

def category(request, category_name_url):
    category_name = decode_url(category_name_url)


                category = Category.objects.get(name=category_name)

    context = {

                "category":category,



        }
    return render(request, "main/category.html", context)

and this is my model for my user from userena.models import UserenaBaseProfile

class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User, unique=True, verbose_name=_('user'), related_name='my_profile')

Upvotes: 0

Views: 100

Answers (2)

ruddra
ruddra

Reputation: 52028

You can add this information in request.session. You can do this like this:

Suppose your login view is this:

def login(request):
    # Do login stuff
    if user.is_active():
        request.session['categories'] = [ c.name for c in Category.objects.filter(author=request.user)] # add to the session 
    return redirect('/somepage')

Display this data in every page like this:

{% for c in request.session.categories %}
{{ c }}
{% endfor %}

And update the category list every time the a new category is added like this:

@login_required
def add_category(request):
    if not request.user.is_superuser and Category.objects.filter(author=request.user).exists():
        return render(request,'main/category_already_exists.html')
    if request.method == 'POST':
        category = Category(author=request.user)
        form = CategoryForm(request.POST, request.FILES, instance=category)
        if form.is_valid():
            form.save(commit=True)
            request.session['categories'] = [ c.name for c in Category.objects.filter(author=request.user)]
            return redirect('category', category_name_url=category.name)

    else:
        form = CategoryForm()
    context = {
        "form":form
    }
    return render(request, 'main/add_category.html',context)

Upvotes: 1

somecallitblues
somecallitblues

Reputation: 733

Perhaps you should write your own context processor and include it in settings. Link to docs https://docs.djangoproject.com/en/dev/ref/templates/api/#context-processors

Definition from Django docs:

"Context processors are functions that receive the current HttpRequest as an argument and return a dict of data to be added to the rendering context.

Their main use is to add common data shared by all templates to the context without repeating code in every view."

Upvotes: 2

Related Questions