Django Widget SelectMultiple

时间:2014-02-05 08:11:56

标签: django select widget admin

我无法使用django admin使用的widget selectMultiple。我的模板上的Form.html,只显示“提交”按钮。如果有人可以帮助我:)这是我的代码。

models.py

class Subjects(models.Model):
    sub_name=models.CharField(max_length=100)
    desc=models.CharField(max_length=100)

class Student(models.Model):
    name=models.CharField(max_length=100)
    subject=models.ManyToManyField(Subjects)

forms.py

class StudentForm(ModelForm):
    class Meta:
        model= Student
        subject=forms.ModelMultipleChoiceField(Subjects.objects.all())


class subAddForm(forms.ModelForm):
    class Meta:
        model = Subjects

views.py

def addSub(request):
    if request.method == "POST":
        form = subAddForm(request.POST)
        if form.is_valid():
            event, created = Subjects.objects.get_or_create(**form.cleaned_data)
            return HttpResponseRedirect('/subject/')
        else:
            return render_to_response('accueil/accueil.html', {'form': form}, context_instance=RequestContext(request))
    else:
        form = subAddForm()
        return render(request, 'accueil/accueil.html', {'form': form})

def form(request):
    stud_form=StudentForm()
    if request.POST:
        stud_form=StudentForm(request.POST)
        stud_form.save()
        return render_to_response("success.html")
    else:
        return render_to_response("test/form.html",{' stud_form': stud_form})


def subject(request):
    subject = Subjects.objects.all()
    return render(request, 'test/subject.html', {'subject': subject})

mon template form.html

    {% extends "base.html" %}

    {% block content %}

        <script type="text/javascript">window.__admin_media_prefix__ = "/static/admin/";</script>

    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/core.js"></script>
    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/getElementsBySelector.js"></script>
    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/actions.js"></script>
    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/SelectBox.js"></script>
    <script type="text/javascript" src="{{ADMIN_MEDIA_PREFIX}}js/SelectFilter2.js"></script>
    <script type="text/javascript" src="/static/js/jsi18n.js"></script>
    <div>
            <div id="overview">
    <form action="" method="POST">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Submit" />
        </form>
        </div>
        </div>

{% endblock %}

1 个答案:

答案 0 :(得分:1)

如果你想要学生和主题的模板,那么在你的student.py的学生功能中,'stud_form'应该是'form'而没有空间