Django请求URL被追加

时间:2018-02-19 11:10:43

标签: python django url-redirection

我访问管理界面并尝试在django.its中添加一个带有用户名和电子邮件的表单的支持用户。表单数据已成功发布。成功发布请求后,我必须重定向到 127.0.0.1:8000/admin/auth/user / 。但是在 127.0.0.1:8000/add_support_user 如果用户已经存在,则url会更改为 127.0.0.1:8000/add_support_user/add_support_user ,如果用户已经再次提出请求存在如下 127.0.0.1:8000/add_support_user/add_support_user/add_support_user (每次都附加)support_User_form,当创建新用户时,url会像这样更改 127.0.0.1 :8000 / add_support_user / add_support_user 因此它显示500错误页面' 127.0.0.1:8000 / add_support_user / add_support_user' 不存在error_page。我在这里犯了什么错误?

URLS.PY

 url(r"^add_support_user/", AddSupportUserView.as_view(), name = 'support_user'),

VIEWS.PY

class AddSupportUserView(CsrfExemptMixin, View):
def get(self, request):
    form_class = AddSupportUserForm

    return render(request, 'add_support_user.html', {
        'form': form_class,
    })

def post(self, request):
    form_class = AddSupportUserForm

    username = request.POST.get('username')
    email = request.POST.get('email')
    try:
        user_obj = User.objects.get(username=username)
        return render(request, 'add_support_user.html', {'errors': 'User already exits', 'form': form_class})
    except User.DoesNotExist:
        user_obj = User.objects.create_user(username=username, email=email, is_staff=True,
                                            is_superuser=False)
        user_obj.set_password(email)
        user_obj.save()
    group_obj = Group.objects.get(name='support_group')
    user_obj.groups.add(group_obj)
    return HttpResponseRedirect('/admin/auth/user/')

CHANGE_LIST.HTML

{% extends "admin/change_list.html" %} {% block object-tools-items %} {{ 
block.super }}
<li>
<a href="{% url 'support_user' %}" class="grp-state-focus addlink">Add   
Support User</a>
</li>
{% endblock %}

ADD_SUPPORT_USER.HTML

{% extends 'admin/base.html' %}{% block content %}
<h1>Add Support User</h1>
<form role="form" action="add_support_user/" method="post">
{% csrf_token %}
<label style="color:red;">*{{errors}}</label>
<table>
    {% for field in form %}
    <tbody>
        <tr>
            <td>
                <label for="{{ field.label }}" style="display:inline- 
block;vertical-align:middle">{{ field.label_tag }}</label>
            </td>
            <td>
                {{ field }}
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
<br>
<input style="float:left;" type="submit" value="Save" class="default"  
name="_save">
</form>
{% endblock %}

1 个答案:

答案 0 :(得分:1)

更改表单

<form role="form" action="add_support_user/" method="post">

到这个

<form role="form" action="" method="post">