English 中文(简体)
Custom traversal and page templates
原标题:

Using Marius Gedminas s excellent blog post, I have created a custom traverser for a folder in my site.

This allows me to show: http://foo.com/folder/random_id

Instead of: http://foo.com/folder/object.html?id=random_id

The configuration side works great, I can catch the random_ids and search through my messages for the correct one, ready to display.

My problem is that I m unsure how to then display the data via my usual page templates - at the TODO point in his original code ;)

if name ==  mycalendar :
            mycalendar = ... # TODO: do something to get the appropriate object
            return mycalendar

Usually I d use something similar to:

class Test(BrowserPage):

    template = ViewPageTemplateFile( atest.pt )

    def __call__(self):
        return self.template()

But I can t work out how to do this correctly in the context of the custom traversal.


UPDATE: To be clear I want to avoid adding anything else to the url (No: http://foo.com/folder/random_id/read).

I don t need the view to be available via any other address (No: http://foo.com/folder/read)

The ZCML for the view I d like to use is:

<browser:page
  for="foo.interfaces.IFooFolderContainer"
  name="read"
  template="read.pt"
  permission="zope.ManageContent"
/>

I m guessing (on further advice), something along the lines of:

return getMultiAdapter((mycalendar, self.request), IPageTemplate, name=u read )

Or even a default view for the object type (a dict in this case) that s being returned:

<browser:page
  for="dict"
  name="read"
  template="read.pt"
  permission="zope.ManageContent"
/>
最佳回答

It would be easier to answer your question if you showed what your custom traverser is doing.

Essentially, you want something like this:

def publishTraverse(self, request, name):
    if name in self.context:
        return MyMessageView(self.context[name], request)

    # fall back to views such as index.html
    view = queryMultiAdapter((self.context, request), name=name)
    if view is not None:
        return view

    # give up and return a 404 Not Found error page
    raise NotFound(self.context, name, request)

where MyMessageView can be something as simple as

class MyMessageView(BrowserPage):
    __call__ = ViewPageTemplateFile( read.pt )

Disclaimer: I m not sure if the view you instantiate directly will be protected by a security wrapper; make sure your functional tests ensure anonymous users can t view messages if that s what you want.

问题回答

If you end up at a proper object with your custom traverser, you can just tack on the template name and user "context" in that template. So http://foo.com/folder/random_id/my_template and in the template do the normal <h1 tal:content="context/title" /> stuff.

IIUC, what you want is to render the read view when somebody requests /folder/random_id. If that s the case, all you need to do is make your traversal return an object (IFolderContent, maybe) representing a random_id and specify the view page as the defaultView for IFolderContent.

The defaultView is needed because there s no view specified for the random_id object in your URL.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签