Can Django fill in a template based on a custom URL, if the contents of the URL match an item in the database?

9 views
Skip to first unread message

Matvey Kostukovsky

unread,
Jul 17, 2019, 11:26:04 AM7/17/19
to Django users
Hi everyone,

This might be a noob question, but I've been searching everywhere for almost half a year now, I've read through a huge chunk of the docs, I've gone through many tutorials, read many stack questions & answers, and I still don't feel like I'm any closer to the solution. I did manage to figure out a few basics and some more complicated set-up type things in the process: I've figure out how to connect Django to an MS SQL Server database on a mac (using the sql_server.pyodbc engine, Docker, Kitematic, and with the help of Azure Data Studio) and to display items from the database by filling in a template using model data. So far so good.

This is a for a diamonds business by the way, and the functionality I'm looking for would allow their team to quickly create eBay listing description HTML from items in the database. I have the HTML template ready to go, and just need to be able to easily send it some data. The admin page is showing the inventory, allowing me to add, delete, and edit items in the database. My goal is the following:
  • create a button on the admin inventory page that would open the custom URL with a ready-to-go eBay listing (which they can copy-paste into eBay when listing an item).. the url would be something like diamondscompany.com/ebay/item123, where item123 is the item's catalogue number
  • (optional) allow their team to navigate to a listing simply by typing that URL in the address bar -- can the template get filled in with all the appropriate data on the spot?
  • it would be nice to save each page as a separate html file for later reference, however this is not required.
I have a feeling that this is some of the most basic functionality of Django, and like one of the main reasons people would use it, however I can't seem to find the right combination of keywords to find documentation on how to set this up, or perhaps I'm just having trouble piecing together everything I've learned so far about views, models, urls, etc.

Any help would be deeply appreciated!

Thanks,

Matvey

Yoo

unread,
Jul 17, 2019, 11:02:38 PM7/17/19
to Django users
Hmm. So is diamondscompany.com/ going to be static? As in that is permanent? If so, then your custom button can simply be a custom URL. I'm not sure if a listing on eBay can automatically be done (not acquainted with it, sorry), but I imagine editing the admin.py with some custom stuff. Maybe read the docs here: https://docs.djangoproject.com/en/2.2/ref/contrib/admin/ See if that helps. 
Maybe you could also dig in and add a custom HTML line like so: <p><a href='https://diamondscompany.com/ebay/{{ item.id }}'>Click here for pre-filled eBay listing.</a></p>

For your second bullet point, there are a couple of things with Django's url stuff to which you can ignore. Here's an example of a blog post: myblog.com/posts/?q=123
In a VIEW in views.py, you can do this:

def posts(request):
    postID = request.GET.get('q')
    post = Post.objects.get(id=postID)
    context = {
       "post": post,
    }
    return render(request, "posts.html", context=context)

this is assuming a URLPATTERN like so (in Django 2.2 at least):
path("posts/", views.posts)
And that's it!

For your last point, regarding saving pages, Django does have a caching mechanism JUST for pages! But I've never tested it. Here's the link: https://docs.djangoproject.com/en/2.2/topics/cache/ (use their table of contents or CTRL + F "template"). Again, I've never tried this, and you may have to ask again if the caches can remain alive forever (maybe use a filebased cache).

I hope that helps. Cheers!
Yoom
Reply all
Reply to author
Forward
0 new messages