Django Admin Action使用中间页面

时间:2015-03-17 22:32:17

标签: django forms admin

我有一个有很多领域的模型。我只有一些我想要的字段。因此,我希望有一个简短的更改列表,而不是更改列表超长,然后有管理操作,可以给出字段的预定义子集。

初始操作将我带到正确的页面,但是当我提交表单时,它会将我返回到我指定的任何页面,但不会更新字段。如果需要的话,我可以再次将其撕下来。我想我真正需要知道的是,我在动作中加入了什么="" html的一部分让递归正常工作?

我正在使用django 1.7。因为我在信息安全领域工作,所以我必须将我的很多领域混淆为cya事物。

这是我的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 个答案:

没有答案