Django传递参数

时间:2012-05-06 01:32:50

标签: django

我有一个页面列表,志愿者列表中的志愿者列表。

Volunteer_list表包含以下字段, Voluntter名称 志愿者职位名称 创建日期

现在从这个页面 - 主页我需要将志愿者职位名称传递给下一页 - 联系页面。志愿者职位字段是50字节字段。

如何定义联系页面的视图以及如何访问联系人页面中的传递字段?

volunteer_list.html

<tr>
        <th colspan=4 align="left"><label for="id_Volposition">Volunteer Position:</label></th>
        <th colspan=.5 align="left"><a href="/signups/new/{{ v.position }}" class="username" <u>{{ v.volposition }}</u></a></th>
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr>
</tr>

如果你注意到这里,我将调用signups / new页面,v.position作为参数传递。我需要知道以下内容,1。如何为注册/新页面定义urls.py访问v.position参数2.如何访问注册/新页面中的参数

我的URLS.py

urlpatterns = patterns('',
   (r'^new/$',                           sfp.view),
   (r'^volunteer/$',     volunteer_page),
   (r'^vollist/$', volunteer_list),
   (r'^volcont/$', volunteer_contact)
)

new / $调用sfp.view ..我的views.py中的一个函数。该视图基本上会呈现一个收集所有信息的html页面。

我需要知道如何将参数“position”传递给这个函数?

这是现有的一段代码..

如果它像教程中给出的直接调用,我没有任何问题。

def detail(request,poll_id):

  • sfp.view的代码

views.py

    sfp = SimpleFormProcessing(
        form_class=VolunteerSignupForm,
        form_2_model=volunteersignupform_2_model,
        form_template='signups/create_contact_form.dmpl',
        email_template='signups/response_email.dmpl',
        email_html_template='signups/response_email_html.dmpl',
        email_subject='Vibha Volunteer Signup',
        email_sender='volunteer@vibha.org',
        redirect_url='/signups/thanks/',
        do_captcha=True)



code for simpleformprocessing:


    class SimpleFormProcessing:

        def __init__(self, form_class, form_2_model, form_template,
                email_template, email_subject, email_sender, redirect_url,
                do_captcha=False, record_ip_addr=False, email_html_template=None):
            self.form_class = form_class
            self.form_2_model = form_2_model
            self.form_template = form_template
            self.email_template = email_template
            self.email_html_template = email_html_template
            self.email_subject = email_subject
            self.email_sender = email_sender
            self.redirect_url = redirect_url
            self.do_captcha = do_captcha
            self.record_ip_addr = record_ip_addr

        def view(self, request, initial={}):
            Form = self.form_class
            if self.do_captcha:
                Form = form_with_captcha(Form, request)
            if self.record_ip_addr:
                Form = form_with_ipaddress(Form, request)
            if request.method == 'POST':
                # Try processing the form
                if self.do_captcha and not accepts_cookies(request):
                    return our_flatpage('Please enable cookies and try again.')
                else:
                    form = Form(request.POST)
                    if form.is_valid():
                        # The form is correct, process it
                        model = self.form_2_model(form)
                        if self.email_template:
                            text_content = render_to_string(self.email_template, {'model': model})
                            recipients = model.emailRecipients()
                            try:
                                bcc_recipients = model.emailBCCRecipients()
                            except:
                                bcc_recipients = None
                            msg = EmailMultiAlternatives(self.email_subject, text_content, self.email_sender,

                                recipients, bcc_recipients)

                        if self.email_html_template:
                            html_content = render_to_string(self.email_html_template, {'model': model})
                            msg.attach_alternative(html_content, "text/html")

                        msg.send()

                    return HttpResponseRedirect(self.redirect_url)
                else:
                    # Show the form with errors
                    return render_to_response(self.form_template, {'form': form})
        else:
            # Show the empty form
            form = Form(initial=initial)
            if self.do_captcha:

1 个答案:

答案 0 :(得分:0)

你想要这样的东西:

from django.views.generic.base import TemplateView

url(r'^/signups/new/?P(<position>\w+)/$', TemplateView.as_view(
    template_name='relative_path_to_template')),

TemplateView类是一个简单的视图,允许您渲染模板。


编辑:

要从视图中访问“position”变量,请定义视图以获取名为position的额外参数:

def view(self, request, initial={}, position=None):
    # The variable is now accessible by calling 'position' as a regular variable.

但请记住,您需要修改我发布的URL路由规则。只需更改第一部分以匹配我给出的URL,例如:

urlpatterns = patterns('',
    url(r'^/new/?P(<position>\w+)/$', sfp.view),
    # ... etc.
)

我没有测试过这个。


总的来说,你在这个问题上几乎没有给我们任何东西。我们希望提供帮助,但我们需要看到您一直在努力制作URL路由规则和视图(而不仅仅是要求我们编写它们)。所有这些信息均可在Django docsofficial Tutorial中找到。我建议先完成本教程。