2

I have a model with a lot of fields. I only have a few fields I that I want to be required. So instead of the change list super long, I want to have a short change list then have admin actions that can give predefined subsets of the fields.

The initial action takes me to the correct page but when I submit the form it returns me to whatever page I designate, but doesn't update the fields. I am okay with tearing this down starting over again if needed. I think what I really need to know, what do I put in the action="" portion of the html to have the recursion work properly?

I am using django 1.7. I have to obfuscate a lot of my fields as a cya thing since I am working in a heavily information secure field.

Here is my admin.py

    class CredentialAdmin(admin.ModelAdmin):
    fields = ['reservedBy','reserveto']
    list_display = ['reservedBy','reserveto']

    class reserveToFormAdmin(forms.Form):
        reservedBy  = forms.CharField(widget=forms.Textarea, max_length=50)
        reserveto = forms.DateTimeField(widget=forms.DateTimeInput)


    def reserveCred(self, request, queryset):
        form = None
        plural = ''
        if 'submit' in request.POST:
            form = self.reserveToFormAdmin(request.POST)
            for f in form.fields:
                print f
            print form.is_valid()
            print form.errors
            if form.is_valid():
                reservetos = form.cleaned_data['reserveto']
                reservedBys = form.cleaned_data['reservedBy']
                print "hello"
                count = 0
                for cred in queryset:
                    cred.reserveto = reservetos
                    cred.reservedBy = reservedByss
                    cred.save()
                    count += 1                  
                if count != 1:
                    plural = 's'
                self.message_user(request, "Successfully reserved %s     cred%s." % (count, plural))
                return HttpResponseRedirect(request.get_full_path(),c)
        if not form:
            form = self.reserveToFormAdmin(initial={'_selected_action' :     request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
        return render(request,'admin/reserveCreds.html',{'creds':queryset,     'form':form, 'path':request.get_full_path()})
reserveCred.short_description = "Reserve Selected Creds"
actions =     [check_out_a_cred,check_in_a_cred,audit_creds,CompareAudits,reserveCred]

reserveCreds.html

{% extends "admin/base_site.html" %}

{% block content %}

<p>How long and which department to reserver creds:</p>

<form action="{{ path }}" method="post">{% csrf_token %}    

    {{ form }}
    <input type="submit" name="submit" value="submit" />
    <input type="button" value = "Cancel" />
</form>
<h2> reserving: </h2>
<ul>
    {% for cred in creds %}
    <li> {{ cred.userid }} </li>
    {% endfor %}
</ul>
{% endblock %}

0 Answers0