Я пытаюсь визуализировать форму в html, но я выдаю ошибку, и у меня нет представления о ее триггере.
Вот ошибка трассировки:
Template error:
In template /Users/usr/Projets/your-query-master/src/core/templates/core/base.html, error at line 18
'ManyToOneRel' object has no attribute 'attname'
8 :
9 : <title>{% block title %} Your-Query {% endblock title %}</title>
10 : <!-- Latest compiled and minified CSS -->
11 : <link rel="stylesheet" href='{% static "css/base.css" %}'>
12 : <link rel="stylesheet" href='{% static "css/bootstrap.min.css" %}'>
13 : <link rel="stylesheet" href='{% static "css/bootstrap-theme.min.css" %}'>
14 : <link rel="stylesheet" href='{% static "css/contest.css" %}'>
15 :
16 : <script src="{% static 'contest_bet.js' %}"></script>
17 :
18 : <link rel="styleshee t" href="htt ps://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
19 : <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
20 : </head>
21 : <body>
22 : {% include "core/navbar.html" %}
23 : <div class="container" style="padding-top: 80px;" >
24 : {% block content %}
25 :
26 : {% endblock content %}
27 : </div>
28 : {% include "core/footer.html" %}
Traceback (most recent call last):
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/usr/Projets/your-query-master/src/questions/views.py", line 78, in contest_bet
return render(request, "questions/contest_bet.html", output)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 992, in render
return render_value_in_context(output, context)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 971, in render_value_in_context
value = str(value)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/utils/html.py", line 373, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/forms/boundfield.py", line 33, in __str__
return self.as_widget()
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/forms/boundfield.py", line 94, in as_widget
value=self.value(),
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/forms/boundfield.py", line 130, in value
return self.field.prepare_value(data)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/forms/models.py", line 1241, in prepare_value
return value.serializable_value(self.to_field_name)
File "/Users/usr/opt/anaconda3/lib/python3.7/site-packages/django/db/models/base.py", line 660, in serializable_value
return getattr(self, field.attname)
Exception Type: AttributeError at /questions/1dcd2c46-8572-11ea-9a09-acde48001122/
Exception Value: 'ManyToOneRel' object has no attribute 'attname'
Так как Я понятия не имею о происхождении проблемы. Я добавил шаблон, модель, форму, URL и код вида.
конкурс. html:
<form method="post" action="/questions/">
{% csrf_token %}
{form.as_ul}
<input class="btn btn-success" type="submit" value='Submit'>
</form>
Модель:
class MyModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
slug = models.SlugField(unique=True, default=uuid.uuid1)
top = models.ForeignKey(Player, on_delete=models.CASCADE, limit_choices_to={'role': "Top"}, related_name="top")
top_score = models.DecimalField(max_digits=6, decimal_places=2, default=0)
jungle = models.ForeignKey(Player, on_delete=models.CASCADE, limit_choices_to={'role': "Jungle"}, related_name="jungle" )
jungle_score = models.DecimalField(max_digits=6, decimal_places=2, default=0)
mid = models.ForeignKey(Player, on_delete=models.CASCADE, limit_choices_to={'role': "Mid"}, related_name="mid")
mid_score = models.DecimalField(max_digits=6, decimal_places=2, default=0)
adc = models.ForeignKey(Player, on_delete=models.CASCADE, limit_choices_to={'role': "ADC"}, related_name="adc")
adc_score = models.DecimalField(max_digits=6, decimal_places=2, default=0)
support = models.ForeignKey(Player, on_delete=models.CASCADE, limit_choices_to={'role': "Support"}, related_name="support")
support_score = models.DecimalField(max_digits=6, decimal_places=2, default=0)
contest = models.ForeignKey(ContestLOL, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
total_score = models.DecimalField(max_digits=6, decimal_places=2, default=0)
def get_absolute_url(self):
return reverse("questions:model_details", kwargs={"slug": self.slug})
class Meta:
verbose_name = "mymodel"
views.py :
def contest(request, slug=None):
contest = get_object_or_404(ContestLOL, slug=slug)
matches= Match.objects.filter(contest=contest.id)
form = MyForm(contest.id, request.POST or None)
if form.is_valid():
mymodel = form.save(commit=False)
mymodel.user = request.user
mymodel.contest = contest
mymodel.save()
messages.success(request, 'Bet was saved')
form = MyForm(contest.id)
output = { "contest": contest,
"form": form,
"matches": matches
}
return render(request, "questions/contest.html", output)
urls.py
urlpatterns = [
url(r'^(?P<slug>[\w-]+)/', contest, name='contest'),
]
Форма:
class MyForm(forms.ModelForm):
def __init__(self, contest_id, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
players= Player.objects.all()
self.fields['top'].queryset = players.filter(role='Top')
self.fields['jungle'].queryset = players.filter(role='Jungle')
self.fields['mid'].queryset = players.filter(role='Mid')
self.fields['adc'].queryset = players.filter(role='ADC')
self.fields['support'].queryset = players.filter(role='Support')
self.fields['top'].initial = players.filter(role='Top')[0]
self.fields['jungle'].initial = players.filter(role='Jungle')[0]
self.fields['mid'].initial = players.filter(role='Mid')[0]
self.fields['adc'].initial = players.filter(role='ADC')[0]
self.fields['support'].initial = players.filter(role='Support')[0]
top = forms.ModelChoiceField(
queryset=Player.objects.filter(role='Top'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='top'
)
jungle = forms.ModelChoiceField(
queryset=Player.objects.filter(role='Jungle'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='jungle'
)
mid = forms.ModelChoiceField(
queryset=Player.objects.filter(role='Mid'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='mid'
)
adc = forms.ModelChoiceField(
queryset=Player.objects.filter(role='ADC'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='adc'
)
support = forms.ModelChoiceField(
queryset=Player.objects.filter(role='Support'),
widget=forms.RadioSelect(),
empty_label=None,
to_field_name='supp'
)
class Meta:
model = MyModel
fields = ('top', 'jungle', 'mid', 'adc', 'support')
Кто-нибудь имеет представление о том, где я должен искать, чтобы решить эту проблему?
Заранее спасибо.