我正在尝试使用Django cities light library,但我遇到一个值错误,如何解决这个问题?

2024-04-18 22:11:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我的应用理念是向客户展示医生的信息,我使用cities light library来获取cities,但是当我尝试使用它时,我得到了一个ValueError。这是我的型号.py地址:

from django.db import models
from django.urls import reverse
from phonenumber_field.modelfields import PhoneNumberField
from cities_light.models import City
# Create your models here.
class Doctor(models.Model):
   name = models.CharField(max_length=100)
   specialty = models.TextField()
   address = models.CharField(max_length=250, blank=True)
   phone_number = PhoneNumberField(blank=True)
   email = models.EmailField(blank=True)
   city = models.ForeignKey(City, on_delete=models.CASCADE)

   def __str__(self):
      return self.name

   def get_absolute_url(self):
      return reverse('home')

这是HTML代码:

{% extends "base.html" %}
{% load static %}
{% block content %}

{% for doctor in object_list %}
    <h1>{{ doctor.name }}</h1>
    <p>{{ doctor.specialty }}</p>
    <p>{{ doctor.address }}</p>
    <p>{{ doctor.phone_number }}</p>
    <p>{{ doctor.email }}</p>
    <p>{{ doctor.city }}</p>

{% endfor %}
{% endblock content %}

错误如下:

Internal Server Error: /patient/
Traceback (most recent call last):
  File "C:\Users\Said\.virtualenvs\mon_psy-XL2vitKu\lib\site- 
 packages\django\template\base.py", line 829, in _resolve_lookup
    current = current[bit]
TypeError: 'Doctor' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Said\.virtualenvs\mon_psy-XL2vitKu\lib\site- 
 packages\django\db\models\fields\related_descriptors.py", line 163, in 
__get__
 rel_obj = self.field.get_cached_value(instance)
 File "C:\Users\Said\.virtualenvs\mon_psy-XL2vitKu\lib\site- 
packages\django\db\models\fields\mixins.py", line 13, in get_cached_value
 return instance._state.fields_cache[cache_name]
KeyError: 'city'

During handling of the above exception, another exception occurred:

ValueError : invalid literal for int() with base 10: 'sdfs'

Tags: djangonameinfrompyimportselftrue