P
P
Pan Propan2016-03-11 00:56:00
Django
Pan Propan, 2016-03-11 00:56:00

How to display a DetailView in Django?

Please help to display the page with the model object.
The model is described as

class Patient(models.Model):
    # uuid = ShortUUIDField()
    firstname = models.CharField(max_length=100, verbose_name="Имя", blank=False, null=True)
    lastname = models.CharField(max_length=300, verbose_name="Фамилия", blank=False, null=True)
    upload_path = 'patient/'
    photo = models.ImageField(upload_to=upload_path, verbose_name="Фото профиля", null=True)
    MAN = 'M'
    FEMALE = 'F'
    GENDER_CHOICES = (
        (MAN, 'Мужской'),
        (FEMALE, 'Женский'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default=MAN, verbose_name="Пол",)
    midle_name = models.CharField(max_length=300, verbose_name="Отчетство", blank=True, null=True)
    birthday = models.DateField(verbose_name="Дата рождения")
    phone = models.CharField(max_length=20, verbose_name="Номер телефона")
    email = models.EmailField(max_length=200, verbose_name="Электронный адрес")

    class Meta:
        verbose_name = 'Пациент'
        verbose_name_plural = 'Пациенты'

    def __str__(self):
        return "%s %s" % (self.firstname, self.lastname)

Next, I created a class in the view:
class PatientDetailView(DetailView):
  model = Patient
  queryset = Patient.objects.filter(id=1)

  def get_context_data(self, **kwargs):
    context = super(PatientDetailView, self).get_context_data(**kwargs)
    context['patient'] = Patient.objects.get(id=id)
    return context

and finally urls
url(r'^(?P<id>[0-9]+)/$', views.PatientDetailView.as_view(), name='patient_detail'),

Trying to open record page 127.0.0.1:8000/patient/1
gives an error
Generic detail view PatientDetailView must be called with either an object pk or a slug.
I understood the essence of the error, how to eliminate it. I have neither slug nor pk in my model.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Rostislav Grigoriev, 2016-03-11
@mgis

What does no pk mean, this is id.
You just need to change the attribute of the view class pk_url_kwarg = 'id', or change the pattern in urls.pyr'^(?P<pk>[0-9]+)/$' to instead of r'^(?P<id>[0-9]+)/$'
Example:

class PatientDetailView(DetailView):
  model = Patient
  pk_url_kwarg = 'id'

D
Dmitry Voronkov, 2016-03-11
@DmitryVoronkov

Delete it. You don't need to write ID yourself. Look at the DetailView source

queryset = Patient.objects.filter(id=1)

  def get_context_data(self, **kwargs):
    context = super(PatientDetailView, self).get_context_data(**kwargs)
    context['patient'] = Patient.objects.get(id=id)
    return context

V
Vladimir Kuts, 2016-03-11
@fox_12

You screwed up something very difficult ...
In addition to describing the model, you need to write something like this in urls.py:

from .views import PatientDetailView
...
    url(r'patient/(?P<pk>\d+)/', PatientDetailView.as_view(), name='patient_detail'),
...

in views.py
from django.views.generic import DetailView
from models import Patient

class PatientDetailView(DetailView):
    model = Patient

And that's all... - an instance of your model with pk=1, when entering /patient/1/, will fly into the context of the patient_detail.html template as an object variable
from django.views.generic import DetailView
from .models import Patient

urlpatterns = [
    ...
    url(r'patient/(?P<pk>\d+)/', DetailView.as_view(model=Patient), name='patient_detail'),
]

Specifically, you have an error due to the fact that you use id in the url instead of pk

V
Viktor Melnikov, 2016-03-11
@Viteran33

I will add, if you need to get an object in some tricky way, then it's better to use get_object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question