我和[@DHerls] [1]有相同的问题,但是给定的解决方案对我不起作用。
我尝试过的其他类似问题以及解决方案:
问题在于,只有一半的FilteredSelectMultiple出现:
我尝试过的事情:
template.html
{{ form.media }}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="{% static 'js/bootstrap.js' %}"></script>
<script src="{% static '/admin/js/jquery.init.js' %}"></script>
<script src="{% static '/js/SelectBox.js' %}"></script>
<script src="{% static '/js/SelectFilter2.js' %}"></script>
<link href="https://fonts.googleapis.com/css?family=Playfair+Display+SC" rel="stylesheet">
<link rel="shortcut icon" type="imgs/favicon.png" href="{% static 'imgs/favicon.png' %}"/>
[...]
<form action="{% url 'recipes' %}" id="add_ingredient" method="post" accept-charset="utf-8" style="width: 400px; margin-left: auto; margin-right: auto; padding: 10px 0 30px 0;" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<span class="input-group-btn">
<button class="btn btn-secondary" style="margin-left: 40%; margin-top: 20px; padding: -10px;" type="submit">Submit</button>
</span>
</form>
views.py
decorators = [login_required, transaction.atomic]
@method_decorator(decorators, name='dispatch')
class RecipeCreate(CreateView):
model = Recipe
form_class = RecipeCreateForm
template_name = 'sous/new_recipe.html'
def get_context_data(self, **kwargs):
context = super(RecipeCreate, self).get_context_data(**kwargs)
context['form'] = RecipeCreateForm()
return context
def form_valid(self, form):
self.object = form.save()
return render(self.request, 'sous/new_recipe.html', {'new_recipe': self.object })
forms.py
class RecipeCreateForm(forms.ModelForm):
name = forms.CharField(max_length=150, required=True, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '*' }))
photo = forms.ImageField(required=False, widget=forms.FileInput(attrs={'class': 'form-control'}))
cost = forms.DecimalField(required=False, min_value=0, max_digits=5, decimal_places=2, widget=forms.NumberInput(attrs={'step': 0.01, 'class': 'form-control', 'placeholder': '00.00'}))
prep_time = forms.DurationField(required=False, widget=forms.NumberInput(attrs={'step': 0.01, 'class': 'form-control', 'placeholder': 'HH.MM'}))
cook_time = forms.DurationField(required=False, widget=forms.NumberInput(attrs={'step': 0.01, 'class': 'form-control', 'placeholder': 'HH.MM'}))
ingredients = forms.ModelMultipleChoiceField(queryset=Ingredient.objects.all(), widget=FilteredSelectMultiple('Ingredient', is_stacked=False, attrs={'rows':'5'}))
class Meta:
model = Ingredient
ordering = ('order',)
fields = ['ingredients', 'name', 'cost', 'prep_time', 'cook_time', 'photo']
css = {
'all':('/admin/css/widgets.css', 'admin/css/overrides.css'),
}
js = ('/admin/jsi18n/',)
def __init__(self, parents=None, *args, **kwargs):
super(RecipeCreateForm, self).__init__(*args, **kwargs)
答案 0 :(得分:0)
使用django 2.2,我可以将其放在我的
中来解决此问题urls.py
urlpatterns = [
...
...
/* all your urls */
re_path(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog')
]
模板底部
<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>
forms.py
class ManageUserPermissionsForm(forms.ModelForm):
class Meta:
model = User
fields = ['permissions']
permissions = forms.ModelMultipleChoiceField(queryset=Permission.objects.all(),required=True,
widget=FilteredSelectMultiple('User Permissions',is_stacked=False))
class Media:
css = {'all': ('/static/admin/css/widgets.css',), }
views.py(您可以忽略必需的权限mixin以及slug_field和slug_url_kwarg
class ManageUserPermissionsView(PermissionRequiredMixin, UpdateView):
permission_required = 'manage_user'
model = User
form_class = ManageUserPermissionsForm
template_name = 'manage_permissions.html'
slug_field = 'uuid'
slug_url_kwarg = 'uuid'
我是专门为管理用户权限而设计的,但是查询集可以随心所欲