Basic note/trello app - how to link model object IDs

66 views
Skip to first unread message

Lylio

unread,
Feb 12, 2018, 4:00:53 PM2/12/18
to Django users
Hi everyone,

I'm new to Django and currently working on a very simple app. It's basically a note app, a bit like a Trello board but aimed at creating user-story cards for software development teams (code at https://github.com/Lylio/scrumbuddy_project).

I made a mock-up in Photoshop of how it's supposed to look:

 



I've got some basic functionality up and running - users can register and create a user-story card. When a new card is created, a title, description, time estimation are inputted. Cards are just displayed as list items in columns. I'm trying to add a button to the cards so that when it's clicked, the details of the card open up in a new and a user can edit the card info or delete it. 

A couple of noob questions:

1. To click on a card so it opens up and displays the details, am I right to say I should create a card.html file in the templates folder for this? 

2. I'm confused about how Django keeps track of the cards in my app - I presume each card object has an ID in the database and I'd need this ID in order to retrieve the card's details when it's clicked - but I'm unsure of how to link the two. In the two pictures above, say someone clicked on the 'class progression' card on the left... I'd like a new page to open up that display the details for that card and allows the user to edit the info or completely delete the card.

I'm maybe asking a bit much here - but I'm unsure of how complex these requirements are. It feels like it should be fairly straightforward. Any thoughts or advice would be greatly appreciated.

Thanks for your time

Gonzalo Delgado

unread,
Feb 13, 2018, 9:18:25 AM2/13/18
to django...@googlegroups.com
Hi Lylio,

On 12/2/18 16:20, Lylio wrote:
> 1. To click on a card so it opens up and displays the details, am I right
> to say I should create a card.html file in the templates folder for this?

It isn't necessary, but it is a good idea
What you need is to create a view that renders such template.
I suggest you write your own view function that does that, but what
you'd usually do is just use a generic view and either pass it the
template you want to use, or name the template in a way the generic view
will find it on its own.
See:
https://docs.djangoproject.com/en/2.0/topics/class-based-views/generic-display/

> 2. I'm confused about how Django keeps track of the cards in my app - I
> presume each card object has an ID in the database and I'd need this
ID in
> order to retrieve the card's details when it's clicked - but I'm
unsure of
> how to link the two. In the two pictures above, say someone clicked on
the
> 'class progression' card on the left... I'd like a new page to open up
that
> display the details for that card and allows the user to edit the info or
> completely delete the card.

Yes, in general, every Django model instance has an id (unless you
specifically tell it not to), and you can use it to create urls for the
detail view I mentioned earlier. This is a good start, but you'll later
want to add a SlugField to your Card model, and use that instead of the
id so urls look nicer.

> I'm maybe asking a bit much here - but I'm unsure of how complex these
> requirements are. It feels like it should be *fairly *straightforward.
Any
> thoughts or advice would be greatly appreciated.

I can assure you using Django for a project like yours is *very*
straightforward.
When in doubt, revisit the Django tutorial. This part is a good one for
where you're at: https://docs.djangoproject.com/en/2.0/intro/tutorial03/


--
Gonzalo Delgado

Lylio

unread,
Feb 14, 2018, 10:14:40 AM2/14/18
to Django users
Thanks for the reply Gonzalo, I really appreciate the feedback.

So, conceptually I understand how the remaining pieces of this app fit together - but could I ask for just a little more of your help as I'm struggling with coding the details of what's needed.

Let's start basic - I've got some 'cards' in my database and on one of the web pages the title of the card is presented in a list (with some CSS styling to present the list item as a rectangle):

<ul>
{% for card in cards.all %}
<li class="card">{{ card.title }} </li>
{% endfor %}
</ul>

I'd like to click on one of the items in the list and open up a new page showing all the database details for that particular list item (title, description, time of creation, etc). Roughly, would I change the list items into links:

<ul>
{% for card in cards.all %}
<li class="card"><a href='card.html/get(this_object_id)>{{ card.title }}</a></li>
       {% endfor %}
</ul>

Then there would be a new view defined, something like

def card(request):
object = get(this_object_id)
context = {'cards': object}
return render(request, 'scrumbuddy/card.html', context)

I know this code is incorrect, but is it roughly how I should proceed? Connecting the list item URL to the card view which I don't really understand how to do.

Many thanks

Lylio

Matthew Pava

unread,
Feb 14, 2018, 10:31:27 AM2/14/18
to django...@googlegroups.com

You’ll want to use card.get_absolute_url.

See https://docs.djangoproject.com/en/2.0/ref/models/instances/#get-absolute-url

In your get_absolute_url, you should probably use the reverse method to use the name of a URL rather than hard-coding it in the method.  That’s the second example in that section.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/eee70b8a-daff-453f-bb46-b4c2769d33e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
Feb 14, 2018, 11:07:32 AM2/14/18
to django...@googlegroups.com
On woensdag 14 februari 2018 16:14:40 CET Lylio wrote:

> I'd like to click on one of the items in the list and open up a new page
> showing all the database details for that particular list item (title,
> description, time of creation, etc). Roughly, would I change the list items
> into links:
>
> <ul>
> {% for card in cards.all %}
> <li class="card"><a href='card.html/get(this_object_id)>{{
> card.title }}</a></li>
>
> {% endfor %}
> </ul>
>
>
> Then there would be a new view defined, something like
>
> def card(request):
> object = get(this_object_id)
> context = {'cards': object}
> return render(request, 'scrumbuddy/card.html', context)
>

> On Tuesday, 13 February 2018 14:18:25 UTC, Gonzalo Delgado wrote:

> > I can assure you using Django for a project like yours is *very*
> > straightforward.
> > When in doubt, revisit the Django tutorial. This part is a good one for
> > where you're at: https://docs.djangoproject.com/en/2.0/intro/tutorial03/


You did not follow this advice, or you would already know how to tie the url
to the primary key of a model instance. It is spelled out there in detail.
--
Melvyn Sopacua

Lylio

unread,
Feb 14, 2018, 11:35:38 AM2/14/18
to Django users
Thanks for the reply Melvyn - I'm slowly getting there!

So, I now understand how to insert the unique object reference. After creating card.html, and defining card in views, my list element URL is:

{% for card in cards.all %}
    <li class="card"><a href="{% url 'card' %}/{{ card.id }}">{{ card.title }}</a> </li>
{% endfor %}
And if I click on the second card object in the list, it opens up http://127.0.0.1:8000/scrumbuddy/card/2 - hooray, progress!

Ok, so now I understand what card.id is and I use this to references the model object instances. So, I presume I need to use that ID to render the details of each card into the body block of card.html.

Would that require editing the card view? All I have at the moment is:

def card(request):
return render(request, 'scrumbuddy/card.html')

So somewhere in here I'd create a reference to card.id, and perhaps pass the id within a context variable?

Thanks again guys, it feels good to be making some progress! Really appreciate your help.

Lylio

unread,
Feb 18, 2018, 4:59:16 PM2/18/18
to Django users
Hi everyone,

I've unfortunately hit another brick wall with my Django app. I have my list of card objects, and when one of them is clicked, I'd like a new page to open that contains the details for the card that was clicked. I found out how to create a URL using {{ card.id }}:

{% for card in cards.all %}
<li class="card"><a href="{% url 'card' %}/{{ card.id }}">{{ card.title }}</a> </li>
{% endfor %}

and in views the card def is simply:

def card(request):
return render(request, 'scrumbuddy/card.html')

So, for example if I click on the third card object in the list, the URL returned is http://127.0.0.1:8000/scrumbuddy/card/3

But I'm unsure of how to populate the http://127.0.0.1:8000/scrumbuddy/card/3 page with the database info for card 3. Hopefully this screenshot can demonstrate:



Reply all
Reply to author
Forward
0 new messages