Search code examples
pythonhtmldjangohref

Django List ID to Form


I apologies if this question has been asked, but I couldnt find anything that could help me solve a small issue.

Ive managed to create a django frame work, and created a model and a form containing data from a csv file.

Basically what im trying to do is:

Ive created a "Home Page", that looks like this(Its just basic to get the idea running): enter image description here Which contains a list of IDS(With username) from the model/form. what I want to be able to do, is that when i click on one of the id's, for it to open my form page,which relates then to only the data of that ID... I hope that makes sense.

So lets say i create a form.html file, and id like it to show all the info in the form/model pertaining to that ID i click on.

Maybe it has something to do with Href link?

I apologies if im not too clear, but please feel free to ask me anything and il do my best to help These are my files:

Model.py:

class datas(models.Model):

    country = models.CharField(_('country'),max_length=200,default='Null') 
    qs_login = models.CharField(_('qs_login'),max_length=200,default='Null') 
    Status = models.CharField(_('Status'),max_length=200,default='Null') 
    seller_id = models.CharField(_('seller_id'),max_length=200,default='Null') 
    Task_ID = models.CharField(_('Task_ID'),max_length=200,default='Null',primary_key=True) 
    associate_queue = models.CharField(_('associate_queue'),max_length=200,default='Null') 
    associate = models.CharField(_('associate'),max_length=200,default='Null') 
    metric_wk_no = models.CharField(_('associate'),max_length=200,default='Null') 
    associate_action = models.CharField(_('associate_action'),max_length=200,default='Null') 
    correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default='Null') 
    Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default='Null') 
    Sub_Key_driver = models.CharField(_('country'),max_length=200,default='Null') 
    Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default='Null') 
    QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default='Null') 
    Metric_name = models.CharField(_('Metric name'),max_length=200,default='Null') 


    investigator_task = models.CharField(_('investigator_task'),max_length=200,default='Null') 
    investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default='Null') 
    investigator = models.CharField(_('investigator'),max_length=200,default='Null') 
    verification_action = models.CharField(_('verification_action'),max_length=200,default='Null') 
    correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default='Null') 
    Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default='Null') 
    Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default='Null') 
    QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default='Null') 
    General_Notes = models.CharField(_('General_Notes'),max_length=200,default='Null') 
    Type_of_audit = models.CharField(_('Type of audit'),max_length=200,default='Null') 

    def __str__(self):
        return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}"
    
    
    #def __str__(self):
    #    return f"{self.country}-{self.qs_login}"
    #    super().save(*args,**kwargs)

Views.py:

def homePage(request):
    model = datas.objects.filter(qs_login='nicobari')
    context = {'items': model }
    return render(request,"main/home.html",context)


def auditFormPage(response):
    model = datas.objects.filter(qs_login='nicobari')
    context = {'items': model }
    return render(response,"main/auditform.html", context)

This is the Home.html where the list is(As you can see i put the item.Task_ID and the item.qs_login:

{% for item in items %}

<div class="container-fluid">
   <li class="nav-item dropdown">
      <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
         <li><a class="dropdown-items" href="#">{{item.Task_ID}}-{{item.qs_login}}<br></a></li>
      </ul>
   </li>
</div>

{% endfor %}

UPDATED:

What i tried to do is create a function in the views.py page:

def auditFormPage(request,pk):
    model = datas.objects.filter(qs_login='nicobari')
    context = {'items': model }
    return render(request,"main/auditform.html", context)

Then i created a pathin the urls.py file which takes a dynamic link with the primary key:

path('AuditForm/<str:pk>/', views.auditFormPage, name='AuditForm'),

and in the dashboard/home i added a href link to the dynamic page:

{% for item in items %}

<div class="container-fluid">
   <li class="nav-item dropdown">
      <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
         <li><a class="dropdown-items" href = "{% url 'AuditForm' item.Task_ID %}">{{item.Task_ID}}-{{item.qs_login}}<br></a></li>
      </ul>
   </li>
</div>

{% endfor %}

But then i get a 404 error as page isnt found(as i guess its looking for AuditForm/str:pk/ but only found AuditForm/):

enter image description here

Models.py:

class datas(models.Model):

    country = models.CharField(_('country'),max_length=200,default='Null') 
    qs_login = models.CharField(_('qs_login'),max_length=200,default='Null') 
    Status = models.CharField(_('Status'),max_length=200,default='Null') 
    seller_id = models.CharField(_('seller_id'),max_length=200,default='Null') 
    Task_ID = models.CharField(_('Task_ID'),max_length=200,default='Null',primary_key=True) 
    associate_queue = models.CharField(_('associate_queue'),max_length=200,default='Null') 
    associate = models.CharField(_('associate'),max_length=200,default='Null') 
    metric_wk_no = models.CharField(_('associate'),max_length=200,default='Null') 
    associate_action = models.CharField(_('associate_action'),max_length=200,default='Null') 
    correct_associate_action = models.CharField(_('correct associate action'),max_length=200,default='Null') 
    Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'),max_length=200,default='Null') 
    Sub_Key_driver = models.CharField(_('country'),max_length=200,default='Null') 
    Defect_Area_Associate = models.CharField(_('Defect Area Associate'),max_length=200,default='Null') 
    QA_Comments_on_Associate_Action = models.CharField(_('country'),max_length=400,default='Null') 
    Metric_name = models.CharField(_('Metric name'),max_length=200,default='Null') 


    investigator_task = models.CharField(_('investigator_task'),max_length=200,default='Null') 
    investigator_queue = models.CharField(_('investigator_queue'),max_length=200,default='Null') 
    investigator = models.CharField(_('investigator'),max_length=200,default='Null') 
    verification_action = models.CharField(_('verification_action'),max_length=200,default='Null') 
    correct_investigator_action = models.CharField(_('correct investigator action'),max_length=200,default='Null') 
    Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'),max_length=200,default='Null') 
    Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'),max_length=200,default='Null') 
    QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'),max_length=200,default='Null') 
    General_Notes = models.CharField(_('General_Notes'),max_length=200,default='Null') 
    Type_of_audit = models.CharField(_('Type of audit'),max_length=200,default='Null') 

    def __str__(self):
        return f"File: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}"

THANKS


Solution

  • Updated answer

    Since you have Task_ID been set primary_key=True.

    path('AuditForm/<str:pk>/', views.auditFormPage, name='AuditForm'),
    

    In order to retrieve a specific object from the model list using that pk passed to the auditFormPage view, you can use the get method on the model list.

    def auditFormPage(request, pk):
        data= datas.objects.get(Task_ID=pk)
        context = {'data': data}
        return render(request,"main/auditform.html", context)
    

    This will match back <a class="dropdown-items" href = "{% url 'AuditForm' item.Task_ID %}">{{item.Task_ID}}-{{item.qs_login}}<br></a>

    OR

    Try retrieving the object not by the primary key but by the id itself...

    path('AuditForm/<int:id>/', views.auditFormPage, name='AuditForm'),
    

    views.py

    def auditFormPage(request, id):
        data= datas.objects.get(id=id)
        context = {'data': data}
        return render(request,"main/auditform.html", context)
    

    within the html

    `<a class="dropdown-items" href ="{% url 'AuditForm' item.id %}">{{item.Task_ID}}-{{item.qs_login}}<br></a>`
    

    That should take you to the page.