点击提交按钮时出现错误的重定向

时间:2016-03-26 18:23:01

标签: python django django-templates django-views

在下面的代码中,点击模板中的发送按钮会将浏览器重定向到" localhost:8000 / new / director" 一个公认的网址模式。我视图中的代码不会启动重定向。

模板

<div class="col-md-8" style="margin-top: 51px; padding: 0px;">
  <p style="font-size: 30px; margin-left: 0px; padding-left: 50px; padding-top: 5px; padding-bottom: 5px; border-bottom: 2px Solid #BEBEBE; width: 100%;"><font color="#9E9E9E">Subject: Circular</font></p>

  <form method="post" action=".">
  <div class="form-group" style="width: 500px; margin-left: 150px; margin-top: 50px;">
    <label for="Regarding">Regarding</label>
      {{ form.regarding }}
  </div>
  </br>
  <div class="form-group" style="width: 500px; margin-left: 150px;">
    <label for="Content">Content</label>
      {{ form.content }}
  </div>
  <div class="form-group" style="margin-left: 150px; margin-top: 20px;">
    <button type="submit" class="btn btn-default"><font color="#333">Send</font></button>
  </div>
  </form>
</div>

查看

def new_circular_director(request):
    template = get_template('new_circular_director.html')

    if User.is_authenticated:
        username = request.user.username
    else:
        return HttpResponseRedirect('/login_board')

    if request.method == 'POST':
        form = new_circularForm(request.POST)

        now = datetime.datetime.now()

        if form.is_valid():
            user = User.objects.get(username = username)
            mem = memo(
                snd_username = user,
                rcv_username = form.cleaned_data['rcv_username'],
                subject = form.cleaned_data['subject'],
                date = str(now.year) + '-' + str(now.month) + '-' + str(now.day),
                time = str(now.hour) + ':' + str(now.minute),
            )

            mem.save()

            mee = mem.circular_set.create(
                snd_username = username,
                rcv_username = mem.rcv_username,
                content = form.cleaned_data['content']
            )

            mee.save()

            return HttpResponseRedirect('/dashboard/director/' + username )

    else:
        form = new_circularForm()


    variables = RequestContext(request, {
        'form': form,
    })
    return render_to_response('new_circular_director.html', variables)

网址

url(r'^new/director/circular', new_circular_director),

表格

class new_circularForm(forms.Form):
    reciever = forms.CharField(
        label='Reciever',
        widget=forms.TextInput(attrs={'placeholder': 'Emp ID', 'class': 'form-control'}),
    )

    content = forms.CharField(
        label='Content',
        widget=forms.Textarea(attrs={'class': 'form-control'}),
    )

    regarding = forms.CharField(
        label='Regarding',
        widget=forms.TextInput(attrs={'placeholder': 'Subject of the Circular', 'class': 'form-control'}),
    )

为什么会出现重定向?

1 个答案:

答案 0 :(得分:1)

.表示操作的含义:转到当前目录的根目录。由于您没有使用斜杠终止路径,因此浏览器会将action="."解释为指示转到&#34; / new / director /&#34;。

解决方案很简单:确保您的网址为"^new/director/circular/$"