gpt4 book ai didi

css - Django FilteredSelectMultiple 右半部分不呈现

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:42 25 4
gpt4 key购买 nike

我遇到了与 [@DHerls][1] 相同的问题,但给定的解决方案对我不起作用。

其他类似问题我试过的解决方案:

问题是只有一半的 FilteredSelectMultiple 出现了:

我尝试过的事情:

  • 同步数据库
  • 检查 jQuery 是否正在运行
  • 检查了 jQuery 导入冲突,但我是新手它,所以我对此不确定。

模板.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>

View .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 })

表单.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)

最佳答案

使用 django 2.2,我可以通过将其放入我的

来解决这个问题

网址.py

urlpatterns = [
...
...
/* all your urls */

re_path(r'^jsi18n/$', JavaScriptCatalog.as_view(), name='javascript-catalog')
]

模板底部

<script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>

表单.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'

我专门为管理用户权限而做的,但是查询集可以是任何你想要的

关于css - Django FilteredSelectMultiple 右半部分不呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51278591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com