Этот вопрос поднимался множество раз, но все они сузились до их конкретной c проблемы. Я надеюсь, что этот вопрос может прояснить проблему больше, поэтому он станет более полезным, чем просто сузится до моей проблемы.
Я получаю сообщение об ошибке 'str' object has no attribute 'field'
И я получил следующий код
template.html
{% load widget_tweaks %}
<form action="." method="POST">
{% render_field form.co_nip_number name="co_nip_number" class="form-control" type="number" required="" %}
</form>
forms.py
from django import forms
from .models import BusinessContact
class BusinessContactForm(forms.Form):
class Meta:
model = BusinessContact
fields = 'co_nip_number'
models.py
from django.db import models
class BusinessContact(models.Model):
co_nip_number = models.CharField(max_length=10)
urls.py
from django.urls import path
from . import views
app_name = 'dashboard'
urlpatterns = [
path('new-client/', views.NewClient.as_view(), name='new-client'),
]
views.py
from django.views.generic import FormView
from django.http import HttpResponse
from .forms import BusinessContactForm
class NewClient(FormView):
template_name = 'dashboard/new-client.html'
form_class = BusinessContactForm
def form_valid(self, form):
form.save()
return HttpResponse('You managed')
TRACEBACK
Traceback (most recent call last):
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/jannicklangkjaer/PycharmProjects/penge/venv/lib/python3.8/site-packages/widget_tweaks/templatetags/widget_tweaks.py", line 220, in render
bounded_field.field.widget.input_type = v.resolve(context)
AttributeError: 'str' object has no attribute 'field'
Что означает эта ошибка? Как мы ее решаем?
Спасибо за ваше время