This is my class from models.py:
class Person(models.Model):
name = models.CharField(max_length=64)
persnr = models.AutoField(primary_key=True)
This is my view:
def show_person(request, persnr):
pers = get_object_or_404(Person, pk=persnr)
context={'pers':pers}
return render(request, 'company/person.html', context)
This is my url:
url(r'^show_person/(?P<persnr>\d+)', "company.views.show_person", name='show_person'),
How can I get to the same view (show_person) with these two urls:
server/show_person/(persnr)
server/show_person/(name)
A slightly different approach would be to accept two separate arguments, one for the pk and one for the name:
url(r'^show_person/(?P<persnr>\d+)', "company.views.show_person", name='show_person'),
url(r'^show_person/(?P<name>\w+)', "company.views.show_person", name='show_person'),
def show_person(request, persnr=None, name=None):
if persnr is not None:
pers = get_object_or_404(Person, pk=persnr)
elif name is not None:
pers = get_object_or_404(Person, name=name)
else:
raise Http404
url(r'^show_person/(?P<persnr>.*)', "company.views.show_person", name='show_person'),
def show_person(request, persnr):
per = Person.objects.filter(Q(name=persnr)|Q(persnr=persnr)).all()
context={'pers':pers}
return render(request, 'company/person.html', context)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With