I got this error:
Internal Server Error: /QOP/1/editar/ Traceback (most recent call last): ... opts = instance._meta AttributeError: 'list' object has no attribute '_meta' [25/Nov/2016 19:17:00] "GET /QOP/1/editar/ HTTP/1.1" 500 77572
models.py
from django.db import models
from smart_selects.db_fields import ChainedForeignKey
from django.core.urlresolvers import reverse
from modulos.modPessoas.models import Postos
# Quadro Orgânico Pessoal
class Qop(models.Model):
class Meta:
ordering = ['qop_ref']
verbose_name = 'Quadro Orgânico Pessoal'
verbose_name_plural = 'Quadro Orgânico Pessoal'
qop_ref = models.CharField(max_length=255, null=True, blank=True, verbose_name='Referência')
qop_nome = models.CharField(max_length=255, null=True, blank=True, verbose_name='QOP Nome')
qop_dt_arov = models.DateField(null=True, blank=True, verbose_name='Data de Aprovação')
qop_atv = models.BooleanField(default=True, verbose_name='Ativo?')
def __str__(self):
return '{} {}'.format(self.qop_ref, self.qop_nome)
def get_absulute_url(self):
return reverse("QOP:detalhe", kwargs={"pk": self.id})
views.py
def qop_editar(request, pk=None):
instance = get_list_or_404(Qop, id=pk)
form = QopForm(request.POST or None, request.FILES or None, instance=instance)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
contexto = {
"titulo": "QOP Editar",
"instance": instance,
"form": form
}
return render(request, "modQOP/editar.html", contexto)
urls.py
from django.conf.urls import url
from .views import (index,
qop_lista,
qop_detalhe,
qop_criar,
qop_editar)
urlpatterns = [
url(r'^$', index),
url(r'^criar/$', qop_criar),
url(r'^lista/$', qop_lista),
url(r'^(?P<pk>\d+)/detalhe/$', qop_detalhe, name='detalhe'),
url(r'^(?P<pk>\d+)/editar/$', qop_editar, name='editar'),
]
template
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
Nome: {{ form.qop_nome }}<br>
Nº de Referência: {{ form.qop_ref }}<br>
Data de Aprovação: {{ form.qop_dt_arov }}<br>
Ativo? {{ form.qop_atv }}<br>
<button type="submit" class="button">Guardar</button>
</form>
What is wrong with code?
Thanks in advance!
edit: Full traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/QOP/1/editar/
Django Version: 1.10.3
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'smart_selects',
'modulos.modAutenticacao',
'modulos.modQOP',
'modulos.modPessoas']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Filipe\envRTm\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\Filipe\envRTm\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Filipe\envRTm\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Filipe\Documents\GitHub\rhead\modulos\modQOP\views.py" in qop_editar
36. form = QopForm(request.POST or None, request.FILES or None, instance=instance)
File "C:\Users\Filipe\envRTm\lib\site-packages\django\forms\models.py" in __init__
282. object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "C:\Users\Filipe\envRTm\lib\site-packages\django\forms\models.py" in model_to_dict
87. opts = instance._meta
Exception Type: AttributeError at /QOP/1/editar/
Exception Value: 'list' object has no attribute '_meta'
You can't pass the result from get_list_or_404 to a ModelForm instance as it will return an QuerySet. You need to use get_object_or_404 to return an instance of the model.