我無法使用由django admin使用的小部件selectMultiple。我的模板上的Form.html僅顯示「提交」按鈕。如果任何人都可以幫助我:)這裏是我的代碼。Django部件SelectMultiple
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})
週一模板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 %}
可能重複[Django的選擇多(HTTP:/ /stackoverflow.com/questions/21565200/django-select-multiple) –