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

Request for help #4

Closed
timrichardson opened this issue Jun 10, 2017 · 3 comments
Closed

Request for help #4

timrichardson opened this issue Jun 10, 2017 · 3 comments

Comments

@timrichardson
Copy link

Hi Dmitri, I'm new to django.
I'm using python 3.6.1 with django 1.11.2 and django_jinja_knockout from git (master).
I am trying to get a simple grid working, the simplest grid as described in the Usage section of the docs.
I am not overriding middleware.py or the context processor.
I have a breakpoint in template_context_process() in your library.
The break point gets hit on requests, such as http://127.0.0.1:8000/admin/

I have defined a url

url(r'^customer(?P<action>/?\w*)/$', SimpleCustomerGrid.as_view(), name='customer_grid',
         kwargs={'view_title': 'Simple customer grid'}),

and the view object is:

class SimpleCustomerGrid(KoGridView):
    model = Customer
    grid_fields = '__all__'
    # Remove next line to disable columns sorting:
    allowed_sort_orders = '__all__'

visiting the url
http://127.0.0.1:8000/customer/
gives this error:

AttributeError at /customer/

'WSGIRequest' object has no attribute 'client_routes'

and the breakpoint on template_context_process() is not being hit.
I have looked and looked for differences between what I'm doing and the sample app, and I can't find the problem. Why is a visit to /customer/ not triggering the breakpoint in template_context_process() ?

@timrichardson
Copy link
Author

I added
'django_jinja_knockout.middleware.ContextMiddleware'

to the list of middleware_classes which have moved me on to the next error.
The documentation in installation doesn't mention this. It refers us to an example settings.py file, but this example is not minimalist: it has an overridden middleware file which is a bit overwhelming for a new-comer.

@Dmitri-Sintsov
Copy link
Owner

Yes, it is not minimalist - I'd say it is a middle-sized pluggable app, it listed in the category "framework" at django packages site. It has AJAX-forms and read-only display forms, besides grids (datatables), also custom querysets implemented. It also has built-in permission middleware for urls.py.

The best way to get used to django-jinja-knockout is to install sample project https://github.com/Dmitri-Sintsov/djk-sample in separate python3 virtualenv. It uses sqlite database thus does not require lots of steps to make it running. It also has working https://github.com/Dmitri-Sintsov/djk-sample/blob/master/djk_sample/settings.py suitable for copy-paste to another Django / Python3 / Jinja projects.

Client routes are url names that are injected to Javascript client-side to make AJAX call urls not hardcoded, but dynamically updated when urls.py changes. These are gathered into current request by https://github.com/Dmitri-Sintsov/django-jinja-knockout/blob/master/django_jinja_knockout/middleware.py then actually injected in https://github.com/Dmitri-Sintsov/django-jinja-knockout/blob/master/django_jinja_knockout/context_processors.py and then rendered by https://github.com/Dmitri-Sintsov/django-jinja-knockout/blob/master/django_jinja_knockout/jinja2/base_min.htm (extend or make custom template to change the layout).

So both middleware and context processor should be included like it is done in djk-sample https://github.com/Dmitri-Sintsov/djk-sample/blob/master/djk_sample/settings.py

Also do not forget that modules, which use django-jinja-knockout should be included into https://github.com/Dmitri-Sintsov/djk-sample/search?l=Python&q=DJK_APPS&type=&utf8=%E2%9C%93 DJK_APPS list that defines which modules will be applied by django-jinja-knockout middleware (django admin modules should not be applied).

However you do not have to override (extend) django_jinja_knockout middleware and context processor as it is done in djk-sample. Just using built-in ones in settings.py is enough:

# DJK_MIDDLEWARE = 'djk_sample.middleware.ContextMiddleware'
DJK_MIDDLEWARE = 'django_jinja_knockout.middleware.ContextMiddleware'

TEMPLATES = [
    {
        "BACKEND": "django_jinja.backend.Jinja2",
        "APP_DIRS": True,
        "OPTIONS": {
            "match_extension": ".htm",
            "app_dirname": "jinja2",
            'context_processors': [
                i18n,
                # 'djk_sample.context_processors.template_context_processor'
                'django_jinja_knockout.context_processors.template_context_processor'
            ]
        },
    },
    {
        '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',
                # Next line is required only if project uses Django templates (DTL).
                'djk_sample.context_processors.template_context_processor'
            ],
        },
    },
]

DJK_APPS = (
    'djk_sample',
    'club_app',
    'event_app',
)

# Order of installed apps is important for Django Template loader to find 'djk_sample/templates/base.html'
# before original allauth 'base.html' is found, when allauth DTL templates are used instead of built-in
# 'django_jinja_knockout._allauth' Jinja2 templates, thus DJK_APPS are included before 'allauth'.
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'sites' is required by allauth
    'django.contrib.sites',
    'django_jinja',
    'django_jinja.contrib._humanize',
    'django_jinja_knockout',
    'django_jinja_knockout._allauth',
) + DJK_APPS + \
(
    'allauth',
    'allauth.account',
    # Required for socialaccount template tag library despite we do not use social login
    'allauth.socialaccount',
)

@Dmitri-Sintsov
Copy link
Owner

Also documentation usually refers to last pypi release, not to git master version of django-jinja-knockout, so it may not always work with master. I try to update documentation only when master is released at pypi.

While djk-sample uses Travis CI and Circle CI tests thus is much more stable than documentation samples.

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

No branches or pull requests

2 participants