Реверс для 'chi_tiet_hop_dong' с ключевыми аргументами '{'so_hd': ''}'' не найден. Проверен 1 шаблон(ы): ['chi_tiet_hop_dong/(?P<so_hd>[^/]+)/$'].

У меня возникла эта ошибка enter image description here

моя модель.

class testimport(models.Model):
    id=models.AutoField(primary_key=True)
    so_hd=models.CharField( max_length=50, unique=True)
    ten_kh=models.CharField( max_length=500)

    def get_absolute_url(self):
        return "/chi_tiet_hop_dong/%s/" % self.so_hd

class report(models.Model):
     id=models.AutoField(primary_key=True)
     so_hd=models.ForeignKey(testimport, on_delete=models.CASCADE, to_field="so_hd")

Если я не использую to_field="so_hd" в модельном отчете, ошибка не появляется, но мне нужно связать его с "so_hd" в модели testimport, а не с "id primary key" в модели testimport без использования to_field

Мое мнение:

def chi_tiet_hop_dong(request,so_hd):
    contract=testimport.objects.filter(so_hd=so_hd)
    print("số hợp đồng trong def chi tiết hợp đồng",so_hd)
    request.session["save_so_hd"]=json.loads(json.dumps(so_hd))
    lst_contract=request.session["get_contract_detail"]
    
    try:
        the_next = lst_contract[lst_contract.index(so_hd) + 1]
        print("the next",the_next)
    except:
        the_next=None

    try:
        the_prev=lst_contract[lst_contract.index(so_hd) - 1]
        print("the prev",the_prev)
    except:
        the_prev=None

    baocao=report.objects.filter(so_hd=so_hd)

    form=AddReportForm()
    
    
    return render(request, "caller_template/contract_detail.html", {"contract":contract,"the_next":the_next,"the_prev":the_prev,"baocao":baocao,"form":form})

Я проверяю вывод the_next, the_prev и so_hd, все в порядке

мой url:

path('chi_tiet_hop_dong/<str:so_hd>/', CallerViews.chi_tiet_hop_dong, name="chi_tiet_hop_dong"),

Пожалуйста, помогите мне

Я думаю, что вы должны попытаться фильтровать по id вместо so_hd, поэтому вы должны сделать это следующим образом в вашем представлении:


def chi_tiet_hop_dong(request,so_hd):
    contract=testimport.objects.filter(id=so_hd)
    print("số hợp đồng trong def chi tiết hợp đồng",so_hd)
    request.session["save_so_hd"]=json.loads(json.dumps(so_hd))
    lst_contract=request.session["get_contract_detail"]
    
    try:
        the_next = lst_contract[lst_contract.index(so_hd) + 1]
        print("the next",the_next)
    except:
        the_next=None

    try:
        the_prev=lst_contract[lst_contract.index(so_hd) - 1]
        print("the prev",the_prev)
    except:
        the_prev=None

    baocao=report.objects.filter(id=so_hd)

    form=AddReportForm()
    
    
    return render(request, "caller_template/contract_detail.html", {"contract":contract,"the_next":the_next,"the_prev":the_prev,"baocao":baocao,"form":form})
Вернуться на верх