1

I want to display data of two joined tables with a foreign key in django ,i have set it all up but it isn't displaying anything , here are my files:

models.py:

from django.db import models
from django.utils.encoding import smart_unicode

class Course (models.Model):
Course_name=models.CharField(max_length=120,null=False,blank=False)
course_id=models.AutoField(primary_key=True)
course_idtomatch=models.IntegerField(max_length=2)

def __unicode__(self):
    return smart_unicode(self.Course_name)

class Subjectsnames(models.Model):
subject_name=models.CharField(max_length=50)
course=models.ForeignKey(Course)


def List item__unicode__(self):
    return smart_unicode(self.subject_name)

views.py:

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from .models import Subjectsnames
from .models import Course


def Subjects(request):
Subject = Subjectsnames.objects.get(id=id)

subjects_data = {
    "subjects_names":Subject
}

print subjects_data
return render_to_response("code.html",subjects_data,context_instance=RequestContext(request))

code.html:

<html>

{% for name in Subjectsnames %}
Name:{{name.subject_name}}
Id:{{name.id}}
{% endfor %}

</html>

And i have these data in my database:

Subjectsnames:

+----+-----------------+-----------+
| id | subject_name    | course_id |
+----+-----------------+-----------+
|  2 | Organic         |         1 |
|  3 | inorganic       |         1 |
|  4 | thermodynacmics |         2 |
|  5 | vectors         |         2 |
|  6 | trigo           |         3 |
|  7 | algebra         |         3 |
|  8 | relational      |         3 |
|  9 | c++             |         4 |
| 10 | c#              |         4 |
+----+-----------------+-----------+

Course:

+-------------+-----------+------------------+
| Course_name | course_id | course_idtomatch |
+-------------+-----------+------------------+
| chemistry   |         1 |                1 |
| pyhics      |         2 |                2 |
| maths       |         3 |                3 |
| computers   |         4 |                4 |
+-------------+-----------+------------------+

Ignore course_idtomatch

As much as i know, i might have done something wrong in for loop in code.html. If anyone knows please help me in solving it, Thanks in advance .

I have updated my views.py something like this :

from django.conf import settings
settings.configure()
from django.shortcuts import render, render_to_response, RequestContext

from django.db import models
from .models import Course
from .models import Subjectsnames

def Subjects(request):
Subject = Subjectsnames.objects.get(id=id)

subjects_data = {
    "subjects_names":Subject
}

print subjects_data
return render_to_response("code.html",subjects_data,context_instance=RequestContext(request))


print Subject

but now when i run python views.py on terminal i get an error like:

Traceback (most recent call last):
File "views.py", line 7, in <module>
from .models import Course
ValueError: Attempted relative import in non-package
Dhruv Narang
  • 161
  • 1
  • 2
  • 11
  • 1
    In python, methods and functions should be lowercase with underscores. Commas and colons should be followed by a whitespace. Assignments should have spaces around the `=`. And, you want to iterate on `subject_names` which is not a queryset but just a single object (`get` method). In your template don't loop, just use `{{subject_names.subject_name}}` etc. Your naming of classes and attributes is a bit funky, also. – Steve K Aug 16 '14 at 09:14
  • @SteveK no, it isn't compulsory, i have checked all of that and included white spaces and also removed loop from template , but nothing is making any difference ,i am able to use admin panel perfectly fine, i have created admin model there and i am able to input objects and display them. White space isn't compulsory `>>> a=1 >>> print a 1 – Dhruv Narang Aug 16 '14 at 10:23
  • I don't say it's compulsory, it's about coding conventions and code readability. If you look at other python code in StackOverflow, you will see the pattern I'm talking about. – Steve K Aug 16 '14 at 18:33

1 Answers1

1

Try this

 Name:{{subjects_names.subject_name}}
 Id:{{subjects_names.id}}

Since you are rendering only one object and not a queryset, you dont need the forloop

And i hope, the id in this line is defined somewhere:

Subject = Subjectsnames.objects.get(id=id)
doniyor
  • 36,596
  • 57
  • 175
  • 260