Home > Enterprise >  Reverse for 'chi_tiet_hop_dong' with keyword arguments '{'so_hd': '�
Reverse for 'chi_tiet_hop_dong' with keyword arguments '{'so_hd': '�

Time:02-20

I have the issue this error enter image description here

my model.

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")

If I dont use to_field="so_hd" in model report, the error doesnt appear, but I need to link it with "so_hd" in model testimport not "id primary key" in model testimport without using to_field

my view:

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})

I check to print out the_next, the_prev and so_hd, it is ok

my url:

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

Please help me

CodePudding user response:

I think you should try to filter by id instead of the so_hd so you would go about it like this in your view:


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})
  • Related