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
  view option in Django Sowmya 0 87 Mar-22-2024, 10:54 AM
Last Post: Sowmya
  context Django Sowmya 0 89 Mar-22-2024, 10:47 AM
Last Post: Sowmya
  CRUD in Update operation in Django Sowmya 2 198 Mar-19-2024, 04:14 AM
Last Post: Sowmya
  For loop in Django Sowmya 0 121 Mar-19-2024, 04:11 AM
Last Post: Sowmya
  Order by Django Sowmya 0 99 Mar-19-2024, 04:08 AM
Last Post: Sowmya
  Python django view error ZeeKolachi 1 233 Mar-18-2024, 03:14 PM
Last Post: Sowmya
  Document on Django Sowmya 0 120 Mar-18-2024, 03:02 PM
Last Post: Sowmya
  Bootstrap Django Sowmya 0 147 Mar-16-2024, 09:22 AM
Last Post: Sowmya
  Fetching Images from DB in Django Dexty 2 1,623 Mar-15-2024, 08:43 AM
Last Post: firn100
  Birçoğundan çoğuna Django wamp sunucusunda çalışmıyor TMSUT 1 420 Feb-20-2024, 01:44 AM
Last Post: wearsafe

Forum Jump:

User Panel Messages

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