1

i visit admin interface and try to add support user in django.its a form with user name and email. the form data is posted successfully. after successful post request i have to redirect to 127.0.0.1:8000/admin/auth/user/. however at 127.0.0.1:8000/add_support_user if user exists already,url is changed like this 127.0.0.1:8000/add_support_user/add_support_user and again i make request if user already exists its like this 127.0.0.1:8000/add_support_user/add_support_user/add_support_user(gets appended every time) support_User_form and when new user is created url is changed like this 127.0.0.1:8000/add_support_user/add_support_user so it shows 500 error page that '127.0.0.1:8000/add_support_user/add_support_user' doesnt exists error_page. what mistake am i making here?

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 %}
6
  • group_obj = Group.objects.get(name='support_group') check this group exists in DB please
    – Nikitka
    Feb 19, 2018 at 11:14
  • @Nikitka why? what does that have to do with the question? Feb 19, 2018 at 11:18
  • @DanielRoseman it's about 500 error
    – Nikitka
    Feb 19, 2018 at 11:26
  • @Nikitka yes suppport_group exists.
    – Ashok
    Feb 19, 2018 at 11:58
  • @Nikitka url redirection is not performed after POST . why?
    – Ashok
    Feb 19, 2018 at 12:00

1 Answer 1

1

change your form

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

to this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.