[Answered ]-Django app view – how to get an entry from the django database in the url and to obtain more information from the database for the view?

2👍

The problem is that your (?P<gene>) is not matching any characters, so you will always have gene='' in your view.

Try changing your url pattern to:

url(r'^genes/(?P<gene>\w+)/$', views.variant_info, name='variant_info'),

This will accept letters A-Z, a-z, digits 0-9 and underscore for the gene argument. I’ve added the trailing slash for consistency with your other views, and the dollar because it’s usually a good idea to do this.

0👍

for the url you’d need something like to match only digits at the end

r'^genes/(?P<gene>TP\d+)$' 

(I think otherwise it will try to match a string and just accept the first match)

as for your second question: sorry, but I don’t get what you’re asking.
But looking at your code I assume you want to filter by properties of the foreign Model:

variant_info = Variant.objects.filter(gene__gene__contains=gene)

This means to search in field gene of object Gene.

ps. that starts to look confusing, I would suggest changing the model to something like

class Gene(models.Model): 
  name = models.CharField(primary_key=True, max_length=110)
  ...  
👤Alex

Leave a comment