[ckan-dev] Customizing Forms in 2.0a
Sean Hammond
sean.hammond at okfn.org
Wed Jan 9 16:32:12 UTC 2013
> The Geo-Plugin works now. But I had to create a new dataset, it didn't
> adopt to the existing ones. I have still issues understanding the
> concept:
Do you mean you continued getting the UndefinedError: 'dict object' has
no attribute 'tracking_summary' for the old datasets, but it worked for
the new ones?
> In the method setup_template_variables() you can add properties to the
> c-Object, which are available In the template. Like in
> c.country_codes. It seems that this just works in the
> new_package_form.html template. In the read template you access the
> data with c.pkg_dict['country_code']. I really don't get it. Why the
> difference. I tried to add some custom data to test the functionality
> e.g. c.test = 'hallo', but this is not working at all. So my
> question: Which data is when passed to the template and how can I
> access it?
Fundamentally, if you want to know which data is passed to which
template, what I do is find out which controller method renders the
template. For example the package read page (e.g. /dataset/my-dataset)
is rendered by ckan/controllers/package.py:PackageController.read().
You can look in ckan/config/routing.py to find out what controller
methods render what URLs. Insert a set trace before the last line of
the method (which should be a return render(... line):
import pdb; pdb.set_trace()
Now restart CKAN, I'm assuming you're running CKAN using paster serve
here and not e.g. Apache, and when you load the package read page you
should see a pdb prompt in your shell where CKAN is running. Then you
can do, for example:
(Pdb) pp dir(c)
to see what is about to be passed to the template in the template
context.
As far as I know there isn't a more convenient way than this to do it,
with the new Jinja setup. And there is no documentation of what is
passed to what templates in the template context. I admit that this
makes template development difficult.
It looks like your setup_template_variables() method _should_ be called
before rendering the read template as well, and therefore any variables
you added to the template context should be available to the read
template. Can you put in a set_trace() or a print statement in your
setup_template_variables() method to check if it is actually being
called?
Can you share your code so far so I can take a look at what's
happening?
c.pkg_dict is something different, that's a dictionary containing your
package's data from the database, CKAN adds that to the template context
(c) before rendering the package read template (and various other
package templates), but when rendering the new package form there won't
be any c.pkg_dict because your package hasn't been created and added to
the database yet. Anyway, c.pkg_dict should be independent of any
variables you add to c in your setup_template_variables() method.
More information about the ckan-dev
mailing list