У меня есть простая модель:
class Test(models.Model):
title = models.CharField(max_length=30);
Форма:
class TestForm(forms.ModelForm):
title=forms.CharField()
И в поле зрения:
class TescCreateView(CreateView):
template_name = 'test.html'
forms=TestForm
success_url = ''
def form_valid(self, form):
return redirect('')
Но у меня есть ошибки:
invalid literal for int() with base 10: 'test'
Traceback: Traceback (последний последний вызов):
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\detail.py", line 106, in get
self.object = self.get_object()
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\detail.py", line 36, in get_object
queryset = queryset.filter(pk=pk)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 844, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 862, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\query.py", line 1263, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\query.py", line 1287, in _add_q
split_subq=split_subq,
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\query.py", line 1225, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\query.py", line 1096, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\lookups.py", line 20, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\lookups.py", line 70, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\triest\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 965, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'test'
[08/Oct/2018 21:46:08] "GET /articles/test HTTP/1.1" 500 124900
Маршруты:
app_name='articles'
urlpatterns = [
path('',ArticleListView.as_view(),name="list"),
path('<pk>',views.DetailView.as_view(),name="detail"),
path('create',ArticleCreateView.as_view(),name="create"),
path('test',TescCreateView.as_view(),name="test"),
path('create2', views.article_create, name="create2"),
]