-3

リクエストに応じて基本的な質問があります。Django に参加してください。以下にフォーム宣言を与えました

ここで、v.position を次のページに渡す必要があります。v.position が設定されている場合、request.get を検証する方法

例: - 以下のコードは正しいですか?

volpostion = request.GET['ボランティアポジション'].strip()

volposition が必要な機能を実行する場合。

その request.Get はフォーム フィールドのディクショナリを取得します。フォーム フィールドはここでラベル名を意味しますか?

Forms.py

<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>

<tr>
        <th colspan=4 align="left"><label for="id_roledesc">Role Description:</label></th>
        <th colspan=.5 align="left">{{ v.roledesc }}</th>
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr>
</tr>

詳しくは:

urls.py

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

ビュー.py

sfp = SimpleFormProcessing(
    form_class=VolunteerSignupForm,
    form_2_model=volunteersignupform_2_model,
    form2_class=VolunteerForm,
    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)

SimpleFormProcessing.py

from django.shortcuts import render_to_response
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.http import HttpResponseRedirect
from formwrappers import form_with_captcha, form_with_ipaddress
import pdb


class SimpleFormProcessing:

    def __init__(self, form_class, form_2_model, form2_class, 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={}):
        pdb.set_trace()
        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:
                accepts_cookies(request)

ボランティアリスト.dmpl

{% extends "base.dmpl" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block contentBox %}
<h2>Volunteer Opportunities</h2>
<p>Vibha is currently looking for volunteers interested in helping the organization in the following areas:</p>
<div class="relation">
<ul>
{% for k in teamrel %}
        <li align="left"><a href="#{{ k }} "> <b> {{ k}} </b> </a> </li>
{% endfor %}
</ul>
</div>

<p>If you are interested in any of these opportunities please contact us by clicking on the position you are interested and filling out the form.</p>
<p>&nbsp;</p>

<table>

{% for v in vollist %}
{% ifchanged  v.teamrelation %}
<tr>
<th colspan=7 align="left"><h3><a name="{{ v.teamrelation }}"> {{v.teamrelation}} </a> </h3></th>
</tr>
{% endifchanged %}


<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>

<tr>
        <th colspan=4 align="left"><label for="id_roledesc">Role Description:</label></th>
        <th colspan=.5 align="left">{{ v.roledesc }}</th>
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr>
</tr>
<tr>
        <th colspan=4 align="left"><label for="id_timereqt">Time Requirements:</label></th>
        <th colspan=.5 align="left">{{ v.noofhours }} hours per month</th>
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr>
</tr>
<tr>
        <th colspan=4 align="left"><label for="id_qualreqt">Qualification and Requirements:</label></th>
        <th colspan=.5 align="left">{{ v.Qualreqt }}</th>
<tr> <td colspan="2" height="2" style="display:none">&nbsp;</td> </tr>
</tr>
<tr>
        <th colspan=4 align="left"><label for="id_duration">Duration of the Role:</label></th>
        <th colspan=20 align="left">  {{ v.Duration}} {{ v.Durationyrmon}}</th>
<tr> <td colspan="10" height="20" style="display:none">&nbsp;</td> </tr>
</tr>
{% endfor %}

</table>

{% endblock %}
4

1 に答える 1

2

あなたの質問は解読するのが非常に難しいですが、私ができることから、ここにいくつかの指針があります:

  1. request.GETと辞書の両方のキーrequest.POST(使用された方法に応じて) はname、フィールドのラベルではなく、フィールドの HTML 属性になります。フィールドを含むコードを投稿しませんでしたが、一般的に、次のようなものがある場合:

    <input type="hidden" name="volposition" value="0">
    

    次に、次のように値を見つけます。

    request.POST['volposition']
    
  2. .get()キーがそこにあるかどうかわからない場合は、常に辞書でメソッドを使用してください。IndexErrorキーが存在しない場合、コードは例外を発生させます。例えば:

    volposition = request.GET.get('volposition')
    
  3. フォームは、事実上常にPOSTメソッドを介して送信する必要があります。まれな例外は検索フォームのようなもので、送信された値をクエリ文字列として持つ方が理にかなっています (これらは通常、GETメソッドを介して送信されます)。経験則として、読み取り専用の目的でリソースをリクエストしている場合は、GET. たとえば、Google を検索しても何も変わりません。検索結果のリストを要求しているだけなので、GETが使用されます。ただし、何らかの変更を行う場合 (セッション データの作成、編集、または更新など、実際に何らかの変更に影響を与えるもの) を使用する場合は、 を使用しますPOST。また、2 つを混ぜてはいけません。たとえばPOST、クエリ文字列を含む URL にはアクセスしないでください。

言ったすべて。次のものでベースをカバーできるはずです。

{% if request.REQUEST.volposition %}
<input type="hidden" name="volposition" value="{{ request.REQUEST.volposition }}">
{% endif %}

request.REQUESTGETまたはによって URL とともに渡されるパラメータが含まれますPOSTデータが送信されるメソッドがわかっている場合は常にrequest.GETorを使用しますが、このような状況では、値が必要なだけで、どちらのメソッドからでも取得できる場合は許容されます。request.POSTrequest.REQUEST

キー存在する場合、非表示の入力がその値でレンダリングされます。データがどのように送信されても​​、volpositionすぐに渡されます。

于 2012-05-07T19:05:00.427 に答える