Python Forum
Django: How to automatically substitute a variable in the admin page at Django 1.11?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Django: How to automatically substitute a variable in the admin page at Django 1.11?
#1
There is a City model. It has a name field. I need to somehow substitute this variable in the admin page in the text field at another models.

Suppose need to write the phrase "Your location is Paris", but it is necessary that the name of the city was substituted by itself (since there are many cities, and prescribing for each is not an option at all). That is, there must be something like "Your location - {City.name}" - that is, a certain city is pulled out of the database and substituted into this line.

Writing a prepared phrase in templates is not an option. I need to do this through the admin panel. Using selects is also not an option, since the number of variables, their location and phrases are not known in advance. For example, the phrase "The capital of France - Paris" (The capital of {Country.name} - {City.name}).

I understand, need to create a shortcode_name field and somehow process it in the view?

I'm use Django 1.11
Reply
#2
It would be better, if you provided some code here; In any case, I think you need to use models.ForeingKey to get desired result.

Hope the following code snippet helps you:

from django.db import models

class City(models.Model):
    name = models.CharField(max_length=10, default='', blank=False)
    area = models.FloatField(default=0.0, blank=True)
    
    def __str__(self):
        return self.name

class Person(models.Model):
    first_name = models.CharField(max_length=10, default='', blank=False)
    where_from = models.ForeingKey(City, null=True, blank=True, on_delete=models.CASCADE)
  
    def __str__(self):
        return self.first_name 

# define other models here if needed   
    
    
#admin.py
#register all models as usual



# views.py
# Let you include Person instance (named instance) to current context and pass 
# this context to template. So, you can get the city name
# in your template, as follows: 
# ---- some template code
# My name is {{ instance.name}}. I am from {{instance.where_from}}. I was born
# in {{instance.where_from.name}}. (This is alternative way to get city's name,
# we don't need to use it, once we've a __str__ method defined in City model)
Reply
#3
Thnxs for help, but it seems you do not quite understand what is required. Here is an example.

location / models.py
from django.db import models

 
class City (models.Model):
    name = models.CharField (max_length = 10, default = '', blank = False)

 
class Country (models.Model):
    name = models.CharField (max_length = 10, default = '', blank = False)
   

metatags / models.py

from django.db import models


class MetaTag (models.Model):
    description = models.TextField ()
Now suppose the admin creates the MetaTag from the admin page. He wants to write a phrase at description field, for example "Paris is not the capital of Germany". But, since there are a lot of cities and countries, need to create only one MetaTag, according to the type "{City.name} is not the capital of {Country.name}" (something similar to the Django template). Thus, only one note of MetaTag is created, and, depending on the conditions at the view, a certain city and a certain country are inserted from the database to this description (the Admin can enter any phrase and any number of variables).
Reply
#4
So, you want to store a Django template in the database. The MetaTag model will be storing a template in the desription field.

from django.template import Context, Template

def view(request):
    val = request.POST.get('some_variable', 'default')  # you may have many parameters here that control selection of City and Country etc.
    q = MetaTag.objects.filter(condition_based_on_the_value).first()
    if q is not None:
        template = Template(q)
        # Loaded q assumed to be a string: "{{capital.name}} is not capital of {{country.name}}" 
       
        # Here you need to get country instance and capital instance. 
        # You can get them based on some parameters passed via POST or GET, 
        # So, we assume that Country and City models are defined
        country = Country.objects.get() #  or filter get country instance
        city = City.objects.get() # or filter, get city instance 
        context = Context({"country": country, "capital": city})
        result = template.render(context)
    # include result to some big_template
    return  HttpResponse(big_template)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  django - add l<a> tag - render_to_string rwahdan 1 282 Jan-12-2022, 08:04 AM
Last Post: SheeppOSU
  django - reset password template rwahdan 2 155 Dec-26-2021, 09:09 PM
Last Post: Jeff900
  register the user as staff member - django rwahdan 0 372 Dec-24-2021, 03:08 PM
Last Post: rwahdan
  Allowing CORS in Django app with a React frontend Dexty 0 142 Dec-22-2021, 11:38 PM
Last Post: Dexty
Shocked Django __init__() got an unexpected keyword argument 'any' ikurorox 2 426 Nov-26-2021, 07:57 PM
Last Post: ikurorox
  Django views : positional argument ghoul 0 250 Nov-15-2021, 06:02 PM
Last Post: ghoul
  <title> django page title dynamic and other field (not working) lemonred 1 385 Nov-04-2021, 08:50 PM
Last Post: lemonred
  Redirect uri mismatch github in django SubratP 0 372 Oct-04-2021, 05:04 PM
Last Post: SubratP
  trying to save data automatically from this page thunderspeed 1 525 Sep-19-2021, 04:57 AM
Last Post: ndc85430
  Django - slide duration rwahdan 4 891 Aug-06-2021, 12:11 AM
Last Post: rwahdan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020