Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Django Admin escaping text

I recently upgraded Django from 1.3 to 1.8.18 and been having issues with links custom made to pre-fill forms in Django admin. For example, I have the following link:

/admin/miscellaneous/whatsnew/add/?title=...%20competition%20results%20uploaded&pub_date=21-04-2017&body=&link=

When executed the pre-filled data in the form looks like: enter image description here

Where it should look like this:

enter image description here

When testing directly from the URL bar in Safari it changes to this after pressing enter:

https://flyball.org.au/admin/miscellaneous/whatsnew/add/?title=...%2520competition%2520results%2520uploaded&pub_date=21-04-2017&body=&link=

models.py

class WhatsNew(models.Model):
    title = models.CharField(max_length=100,help_text='Title, MAX 100 characters.')
    body = models.TextField()
    pub_date = models.DateField()
    message_expiry = models.DateField(default=datetime.date.today() + relativedelta(years=1))
    link = models.URLField(blank=True, null=True)

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = "Whats New?"

    def __unicode__(self):
        return self.title

admin.py

import models
from django.contrib import admin

class WhatsNewAdmin(admin.ModelAdmin):
    list_display = ('title','pub_date','message_expiry','link','body')

admin.site.register(models.WhatsNew, WhatsNewAdmin)

What can I do to resolve this?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

So, I'm not sure how to do it on the ModelAdmin, but you can create custom setters on your model to handle this situation. Here's how I would go about escaping the URL encoded strings:

import urllib


class WhatsNew(models.Model):
    # Field with custom setter
    _title = models.CharField(max_length=100,
                             help_text='Title, MAX 100 characters.',
                             db_column='title')

    body = models.TextField()
    pub_date = models.DateField()
    message_expiry = models.DateField(default=datetime.date.today() + relativedelta(years=1))
    link = models.URLField(blank=True, null=True)

    # Custom getter and setter
    @property
    def title(self):
        return self._title

    @title.setter
    def title(self, value):
        self._title = urllib.unquote(value)

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = "Whats New?"

    def __unicode__(self):
        return self._title
over 4 years ago · Santiago Trujillo Relatório

0

Use + instead of %20 for space and it works.

Your link should be something like:

/admin/miscellaneous/whatsnew/add/?title=...+competition+results+uploaded&pub_date=21-04-2017&body=&link=
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda