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
  Redirect uri mismatch github in django SubratP 0 107 Oct-04-2021, 05:04 PM
Last Post: SubratP
  trying to save data automatically from this page thunderspeed 1 172 Sep-19-2021, 04:57 AM
Last Post: ndc85430
  Django - slide duration rwahdan 4 460 Aug-06-2021, 12:11 AM
Last Post: rwahdan
  help on django issue jip31 1 485 May-20-2021, 03:42 PM
Last Post: jip31
Lightbulb Hypertag. New language for HTML templating w/ Django support mwojnars 0 547 Apr-06-2021, 12:53 PM
Last Post: mwojnars
Photo After using models.Model on my class Im getting 'ImproperlyConfigured' error Django khavro 1 560 Apr-05-2021, 03:11 PM
Last Post: SheeppOSU
  Django project deployed to Heroku: Postgres security Drone4four 0 601 Mar-26-2021, 10:17 AM
Last Post: Drone4four
  Django : OperationalError no such column: Utilisateurs_videos.user_id Adem 0 682 Mar-20-2021, 06:11 PM
Last Post: Adem
  Upgraded Django, `AdminSplitDateTime` causes exception on `list` object 1780yz 2 968 Feb-24-2021, 04:47 PM
Last Post: 1780yz
  DJANGO Looping Through Context Variable with specific data Taz 0 540 Feb-18-2021, 03:52 PM
Last Post: Taz

Forum Jump:

User Panel Messages

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