Using query string in pyramid python route configuration

This is very specific to what I'm trying to do. I am starting to describe what it is:

  • application writing Pyramid application, e.g. http: // localhost: 6543 / path / to / myplot / plot001.png
  • if the graph is not available, another image is displayed (work.png)
  • The other part is the warp view, which provides an HTML form for entering configuration for the plot, such as http: // localhost: 6543 / path / to / myplot / plot001.png? Action = edit . Note the query string "action = edit".
  • The configuration consists of a data file, templates, etc.
  • the form saves (to save the configuration) the render buttons (http: // localhost: 6543 / path / to / myplot / plot001.png? action = render). Mapping the results to a png file, which is then used in a static way.

I figured out all the parts, such as rendering using Matplotlib, etc., but I'm new to Pyramid and Deform. I also have a working view that serves the plot from the file. Deformation is also a type of work. At the moment, I do not understand how to best structure ULR in order to distinguish services, edit and make usecases. I think in Pyramid this means how to set up routes for serve_view and edit_view.

__init__.py: config.add_route('serve_route', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') config.add_route('edit_route', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') # can I use query strings like "?action=edit" here to distinguish the difference? views.py: @view_config(context=Root, route_name='serve_route') def plot_view(context, request): ... @view_config(context=Root, renderer='bunseki:templates/form.pt', route_name='edit_route') def edit_view(request): ... 

In the Pyramid manual, I could not find a link on how to set parameters in the route. I assume that a pointer to some documentation or sample will be sufficient, and I myself can figure out the details. Thanks!

+6
source share
3 answers

There are two ways to do this, depending on what you prefer to split the code.

  • Put all the logic in your view, separated by 'if' statements on request.GET.get('action') .

     config.add_route('plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') config.scan() @view_config(route_name='plot') def plot_view(request): action = request.GET('action') if action == 'edit': # do something return render_to_response('bunseki:templates/form.pt', {}, request) # return the png 
  • Register several species and delegate them among themselves using the search mechanics in Pyramid mode.

     config.add_route('plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') config.scan() @view_config(route_name='plot') def plot_image_view(request): # return the plot image @view_config(route_name='plot', request_param='action=edit', renderer='bunseki:templates/form.pt') def edit_plot_view(request): # edit the plot return {} # etc.. 

Hope this helps. This is a great example of registering a single url pattern and using different views for different types of requests at this URL.

+11
source

I'm not sure if you can use contex=Root in this situation, but what you are asking for is perhaps a matchdict .

.

__ __ INIT ru:

 config.add_route('serve_route', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') 

views.py:

 @view_config(route_name='serve_route') def plot_view(request): project_name = request.matchdict['project_name'] action = request.params.get('action', None) 

http://docs.pylonsproject.org/projects/pyramid/1.1/narr/urldispatch.html#matchdict

Edit:

If your question is a more general question regarding routing, you should create one route for each action so that your view code is shorter and more understandable. For example, if you want to edit and visualize, your routes might look something like this:

.

__ __ INIT ru:

 config.add_route('render_plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') config.add_route('edit_plot', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}/edit') 

views.py:

 @view_config('render_plot') def render(request): pass @view_config('edit_plot', renderer='bunseki:templates/form.pt') def edit(request): pass 
+1
source

A more efficient way would be to specify the action in the URL. And you can even perform different actions with the same route name or several.

 config.add_route('serve_route', '/{project_name}/testruns/{testrun_name}/plots/{action}/{plot_name}.png') views.py @view_config(context=Root, route_name='serve_route', action='view') def plot_view(request): pass 

Or with a query string

 `config.add_route('serve_route', '/{project_name}/testruns/{testrun_name}/plots/{plot_name}.png') views.py @view_config(context=Root, route_name='serve_route') def plot_view(request): try: action = getattr(self._request.GET, 'action') except AttributeError: raise 
0
source

Source: https://habr.com/ru/post/894018/


All Articles