Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 7: context processors does not work #62

Open
Rahimz opened this issue Feb 1, 2021 · 2 comments · Fixed by #90
Open

Chapter 7: context processors does not work #62

Rahimz opened this issue Feb 1, 2021 · 2 comments · Fixed by #90

Comments

@Rahimz
Copy link

Rahimz commented Feb 1, 2021

When I add this line in my settings to use context processors for using cart info in all templates:

'context_processors': [
#....
'cart.context_processors.cart',

I get ModuleNotFoundError. It says: No module named 'cart.context_processors'.

Does anyone see this error?

@qodatecnologia
Copy link

Yeah! i solved that! the book has a too much bad code with syntax errors. On of them is the name of context processors. In book: "context_ processors.py" but the right is "context_processors.py" without that space after underscore. Try this!! and be careful with the codes: too many basic errors

@zenx zenx linked a pull request Jan 31, 2022 that will close this issue
@lukstaron
Copy link

lukstaron commented Nov 1, 2022

Hi Antonio, I make the shop on django 3.2 version (as 3.0.9 is no longer supported), I am not getting this context processor to work - despite i did everything by the book, I am getting 'Your cart is empty' in the template despite adding items to the cart.

I solved previous error with the wrong filename but here I am not getting why....

This code is so simple :) what can go wrong?

Settings:

    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'cart.context_processors.cart',
            ],
        },
    },
]

Shop/templates/shop/base.html:

            {% with total_items=cart|length %}
            {% if total_items > 0 %}
              Your cart:
              <a href="{% url "cart:cart_detail" %}">
                {{ total_items }} item{{ total_items|pluralize }},
                ${{ cart.get_total_price }}
              </a>
            {% else %}
              Your cart is empty.
            {% endif %}
          {% endwith %}
        </div>

cart/context_processors.py:



def cart(request):
    return {'cart': Cart(request)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants