0

OK, I think this is actually simpler than I'm making it, but I can't figure out what to search for in the django documentation.

Here's what I need to do. I'm about to run a script (using the writing custom django-management commands guide) which will add a whole bunch of records to my database. A mass import of all the student data. After I create these 130 new records, I need to create a QR code for each of them. I've found several QR code generator sites, but that's slow as hell and the school won't pay for me to sign up for the ones that come with mass QR code generation. No problem, this is Python. Batteries included!

I have the qrcode module installed, but what I don't understand is how I can get the URL after it creates the object. I know which view I want (the relative url is /awards/student/<student_id>/). I know that in the HTML template language, that {% url 'StudentHome' student.id %} gets me that url. Can I access that from the command line and pass it to qrcode, which would then generate and save the qrcodes as png files?

0

1 Answer 1

4

Use reverse method to reverse a URL.

reverse("url_name", args=(args1))

As for your case

reverse("StudentHome", args=(student.id))

For getting absolute Uri you can use this method.

  abs_uri = request.build_absolute_uri(reverse("StudentHome", args=(student.id)))

Refs: docs

Sign up to request clarification or add additional context in comments.

3 Comments

Does that give me an absolute url (www.mysite.com/awards/student/1/) or a relative url (/awards/student/1/)?
It can give only relative uri. If you want to build absolute Uri then you will need request object. I will edit the answer for it
God Bless you ,your response is very useful.i was having the same problem and your answer solved it )))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.