0

Let me ask about a python pyramid framework. I want to perform the logic (inspection, registration, redirection) of the form to display with all pages in one place of the view. I use the panel of pyramid_layout, but the panel cannot return HTTPFound(). It seems to be able to come true in hook or javascript, but is there not the good method?

  • pyramid==1.4.5
  • WTForms==1.0.5

Thanks, comment.

this is common footer with feedbackform.

app/tempaltes/layouts/layout.mako

${panel('navbar')}
${next.body()}
<!-- common footers -->
<div class="container">
${panel('common_footers')}
    <div class="col-lg-5">
        <p>Please drop us a line on Project.</p>
        <form id="feedback" action="${action}" method="post">
            <div class="form-group${' has-error' if form.feedback.errors else ''}">
                ${form.feedback(class_="form-control", rows="4", maxlength="140")}
            </div>
            % if form.feedback.errors:
            <div class="form-group has-error">
                <p class="text-danger">
                % for error in form.feedback.errors:
                    ${error}
                % endfor
                </p>
            </div>
            % endif
            <div class="form-group">
                <button type="submit" class="btn btn-primary">Send us a feedback</button>
            </div>
        </form>
    </div>
</div><!--/.container -->

and this is views.py

app/views.py

@view_config(route_name = 'home', renderer = '/home.mako')
def home(request):
    form = Feedback(request.POST)
    url = request.current_route_url()
    if request.method == 'POST' and form.validate():
        return HTTPFound(location = url)
    return dict(form = form, action = url)

def foo(request):
    ... same code

def bar(request):
    ... same code

It cannot be necessary to write the same code to a function in view.py many times. Is it wrong?

user2897065
  • 95
  • 1
  • 1
  • 4

1 Answers1

0

You can stack multiple decorators on top of the @view_config(route_name='home', renderer='/home.mako')

Something like:

@view_config(route_name='foo', renderer='/home.mako')
@view_config(route_name='bar', renderer='/home.mako')
@view_config(route_name='home', renderer='/home.mako')
def home(request):
    form = Feedback(request.POST)
    url = request.current_route_url()
    if request.method == 'POST' and form.validate():
        return HTTPFound(location = url)
    return dict(form = form, action = url)

You can find more information here.

Raj
  • 1,479
  • 3
  • 15
  • 29
  • I see! beatiful. I read a document well. Raj, Thank you very much! – user2897065 Mar 05 '14 at 13:36
  • When it becomes the premise that renderers are different of @view_config decorater stacked, so conflict in the advance. When I want to use the different template in each route. – user2897065 Mar 06 '14 at 02:44
  • http://stackoverflow.com/questions/8573149/easy-way-to-switch-between-renderers-within-the-same-view-method – Raj Mar 06 '14 at 15:37
  • No wonder. If even if route_name is the same, renderer is different of decorator pile it up, and raising is possible. I performed it until the validation of the form in panel of pyramid_layout and coped in jquery. – user2897065 Mar 10 '14 at 03:01