В моем терминале отображаются значения имени и оценки, но как сохранить эти два значения переменных в конкретной модели (Результаты).
models.py:
class Results(models.Model):
username = models.CharField(max_length=50,default='')
score = models.IntegerField(null=True,blank=True)
views.py:
@login_required
def html2(request):
name = request.GET.get('name')
scores = request.GET.get('scores')
print(name, scores)
save_data = Results.objects.create(username=name, score=scores)
context = {
'data': save_data
}
return render(request,'quiz/html/html2.html',context)
Терминал:
[29/May/2020 13:15:22] "GET /api2/?format=json HTTP/1.1" 200 1467
Pavana 40
Я пробовал этот оператор, но он отображает ошибка ограничения not null , поэтому я удалил этот оператор Ошибка:
Traceback:
File "C:\Python38-32\lib\site-packages\django\db\backends\utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "C:\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
296. return Database.Cursor.execute(self, query, params)
The above exception (NOT NULL constraint failed: quiz_results.username) was the direct cause of the following exception:
File "C:\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python38-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\user\dev\samplequiz\quiz\views.py" in html2
58. save_data = Results.objects.create(username=name, score=scores)
File "C:\Python38-32\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python38-32\lib\site-packages\django\db\models\query.py" in create
413. obj.save(force_insert=True, using=self.db)
File "C:\Python38-32\lib\site-packages\django\db\models\base.py" in save
717. self.save_base(using=using, force_insert=force_insert,
File "C:\Python38-32\lib\site-packages\django\db\models\base.py" in save_base
748. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python38-32\lib\site-packages\django\db\models\base.py" in _save_table
831. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Python38-32\lib\site-packages\django\db\models\base.py" in _do_insert
868. return manager._insert([self], fields=fields, return_id=update_pk,
File "C:\Python38-32\lib\site-packages\django\db\models\manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python38-32\lib\site-packages\django\db\models\query.py" in _insert
1136. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python38-32\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
1289. cursor.execute(sql, params)
File "C:\Python38-32\lib\site-packages\django\db\backends\utils.py" in execute
100. return super().execute(sql, params)
File "C:\Python38-32\lib\site-packages\django\db\backends\utils.py" in execute
68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Python38-32\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
77. return executor(sql, params, many, context)
File "C:\Python38-32\lib\site-packages\django\db\backends\utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "C:\Python38-32\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Python38-32\lib\site-packages\django\db\backends\utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "C:\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
296. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /
Exception Value: NOT NULL constraint failed: quiz_results.username
save_data = Results.objects.create(username=name, score=scores)
Как мне сохранить имя и оценки в модели результатов.