How to get object id from database?

20 views
Skip to first unread message

anchal agarwal

unread,
May 25, 2019, 2:46:58 PM5/25/19
to django...@googlegroups.com
I am currently working on a django project in which there is a music app. The models of this app contains Albums and Songs. I want to return the http response when the django receives a url as /music/712 , here 712 is the object id.
Please help me out in resolving the issue.Thanks in advance.

Here are my files. Here album_id will be the id of the object.
models.py
from django.db import models

class Album(models.Model):
artist = models.CharField(max_length=250)
album_title = models.CharField(max_length=500)
genre=models.CharField(max_length=100)
album_logo = models.CharField(max_length=1000)

def __str__(self):
return self.album_title

class Song(models.Model):
album= models.ForeignKey(Album, on_delete=models.CASCADE)
file_type= models.CharField(max_length=10)
song_title = models.CharField(max_length=250)

views.py
from django.shortcuts import render
from django.http import HttpResponse

def homepage(request):
return HttpResponse("<h1>You are looking music!!")

def detail(request, album_id):
return HttpResponse("<h2>Details for Album id:"+ str(album_id)+ "</h2>")

urls.py
 
from django.urls import path
from . import views

app_name="music"

urlpatterns=[
#/music/
path('',views.homepage,name='homepage'),

# /music/712
path('<album_id>[0-9]+/',views.detail,name='detail'),
]

Yoo

unread,
May 25, 2019, 7:25:30 PM5/25/19
to Django users
Maybe try album__id (two underscores)? Or try album_title as defined in the Album model. I also advise not to expose your primary key in the URL. If the above did not work, check Django  documentation: https://docs.djangoproject.com/en/2.2/topics/http/urls/

Rob Gmail

unread,
May 25, 2019, 7:35:43 PM5/25/19
to django...@googlegroups.com
Look at the new Boston.com he did a tutorial on exactly this issue

Rob 
Sent from my mobile device, please excuse the typos. 
--
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/848dc27b-9d4c-42a1-a691-c74ac4112e41%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

anchal agarwal

unread,
May 26, 2019, 2:01:06 AM5/26/19
to django...@googlegroups.com

Thanks . It worked !!

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages