简体   繁体   中英

Django, link to object in db

I've got a django test-project going with a postgresql db with a table called customers. Rendering all the customers in /customers/ is fine but I'm looking for a way to render a single customer like this: /customer/{{customer.id}} . Not really sure what I'm looking for, anyone that can guide me to the correct documentation or something?

Much appreciated.

List all customers and open given customer when clicking on the div:

<div class="container">
    {% for customerz in custs_all %}
    <br>
    <div class="row shadow-sm d-flex w-1000 justify-content-between" onclick="location.href='/customers/{{customerz.id}}';" style="cursor: pointer;">
      <div class="col-xl-4"><h5 class="mb-1">{{ customerz.name }}</h5></div>
      <div class="col-xl-4">Contact person: {{ customerz.contactperson }}</div>
      <div class="col-xl-4">Email: {{ customerz.email }}</div>
      <div class="col-xl-4">Active: <span class="badge badge-light">{{ customerz.active }}</span></div>
    </div>
    {% endfor %}
</div>

So this is what I dont understand, what doc do I need to read to setup this to work?

onclick="location.href='/customers/{{customerz.id}}';"

I dont know what I'm looking for, so its hard to ask the right questions here.. Sorry

I think you are looking for this . You can pass an ID in the URL request and use it to get your desired object from the database.

views.py

def customer_detail(request, pk):
  customer = get_object_or_404(Customer, id=pk)
  return render (request, template, {'customer': customer}

Here pk is primary key so you are getting a customer from your model Customer(or whatever you have named it).

urls.py

path('customer/<int:pk>/', views.customer_detail, name='customer-detail')

And in cutomer list template you can add link to add you customer detail template like <a href="{% 'customer-detail' cutomer.pk %}"> cutomer detail </a>

        The previous answer was correct, in action doe ; you could do something like this :
     
    -- you could grab the customer id through request.form; if you use form within your table; i guess this is the best approach to do so.
    -- than you can set something with (url_for), and give it a parameter as {{ customer.id}} , within your .html . 
    -- than handle the logic when returning this object.
    
    You can check this project, go through it , you might find something similar to this : 
     [https://github.com/Nouamanezh909/BLog_repo][1]

urls.py

path('customer/<int:pk>/', views.customer_detail, name='customer-detail'),

views.py

def customer_detail(request, pk):
  customer = Customers.objects.get(id=pk)
  return render (request, 'customer.html', {'customer': customer})

customer.html

{% extends "base.html" %}

{% block content %}
<br>

<div class="container row">
    <div class="col-xl-12"><h5 class="mb-1">{{ customer.name }}</h5></div>
    <div class="col-xl-12">Active: <span class="badge badge-light">{{ customer.active }}</span><hr></div>
    <div class="col-xl-12">Description: {{ customer.desc }}<hr></div>
    <div class="col-xl-12">Contact person: {{ customer.contactperson }}</div>
    <div class="col-xl-12">Email: {{ customer.email }}</div>
    <div class="col-xl-12">Address: {{ customer.addr }}</div>
    <div class="col-xl-12">Last updated: {{ customer.modified }}</div>
</div>

<br>
{% endblock %}

This is currently doing exactly what i want:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM