我正在創建一個工作板網站,其中所有工作都顯示在列表中,每個工作鏈接都會導致顯示有關工作更多信息的頁面。外鍵對象是否有唯一的ID值?
我有以下型號:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Employer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return self.user.first_name
@receiver(post_save, sender=User)
def create_employer(sender, instance, created, **kwargs):
if created:
Employer.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_employer(sender, instance, **kwargs):
instance.employer.save()
class Job(models.Model):
poster = models.ForeignKey(Employer, on_delete=models.CASCADE)
job_title = models.CharField(max_length=50)
establishment_name = models.CharField(max_length = 50)
address = models.CharField(max_length = 50)
state = models.CharField(max_length = 20)
zip_code = models.CharField(max_length = 10)
def __str__(self):
return self.job_title + " - " + self.establishment_name \
+ ", " + self.poster.user.first_name + " " +self.poster.user.last_name
Views.py:
def index(request):
jobs = Job.objects.all
return render(request, 'core/index.html', {'jobs' :jobs })
在index.html的,可用的工作被列爲如此:
<table>
<tbody>
{% for job in jobs %}
<tr>
<td><a href="#">{{ job.job_title}} - {{ job.establishment_name }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
我想這些是可點擊的鏈接,這將使用戶進入另一個頁面,給他們更多關於工作的細節。我想我需要某種身份證號碼作爲Job
對象才能使這成爲可能,但我可能是錯的。有人知道如何去做這件事嗎?注意:我之前做過類似於用戶ID的事情:url(r'^claim/(?P<user_id>[0-9]+)/$', views.ClaimView.as_view(), name = "claim"),
,但我如何使用Job
對象執行此操作?
很有見地,謝謝! –