我需要返回完成ModelForm后生成的链接

2024-10-03 13:28:53 发布

您现在位置:Python中文网/ 问答频道 /正文

部分表单.py你知道吗

公共类(窗体.ModelForm)地址:

class Meta:
    model = Publicacao
    exclude = ('usuario', 'aprovado', 'cadastrado_em', 'slug')

def enviar(self):
    titulo = 'mensagem enviada pelo site'
    destino = self.cleaned_data['emailp']
    mensagem = u"""
    Message that will be sent after completing the form.
    Here I must pass a link to the full URL into the body of the email, something like:
    [ 1 ]http://www.domain.com/item/playstation3/
                         /view/slug/   
    """  % self.cleaned_data

    send_mail(
        subject = titulo,
        message = mensagem,
        from_email = 'inform@domain.com',
        recipient_list =[destino],
        )

[1]我读到关于“反向”的内容,尝试挂载url+view+parameter。 但是我不能正确生成链接,做了很多方法但是不能。你知道吗

我需要传递域名+视图+参数slug是完成表单后生成的。你知道吗

有关电子邮件收件人的信息,请参阅正确的链接。你知道吗

有人能帮我吗? 提前谢谢。你知道吗


Tags: thepyselfcomview表单data链接
2条回答

使用reverse()通常是生成不带域的URL部分的正确方法。例如,如果您的URL配置包含以下内容:

(r'^item/(?P<item>[-%\w]+)/view/(?P<slug>[-\w]+)$', 'my_view_function')

接下来的电话

reverse('my_view_function', kwargs={'item': 'playstation3', 'slug': 'my-slug'})

应该返回/item/playstation3/view/my-slug。你知道吗

最好,您的模型Publicacao应该定义一个get_absolute_url方法,该方法返回模型实例的实际URL。见http://docs.djangoproject.com/en/1.2/ref/models/instances/#get-absolute-url。你知道吗

可以使用站点框架检索域名部分:

>>> from django.contrib.sites.models import Site
>>> s = Site.objects.get_current()
>>> s.domain
u'localhost:8000'

当然,您必须正确配置站点的域。另见http://docs.djangoproject.com/en/1.2/ref/contrib/sites/#getting-the-current-domain-for-full-urls。你知道吗

伯恩德,谢谢。 关于“反面”我已经知道了。你知道吗

一个简短的例子。 这只是鉴定的问题。http://pastebin.ca/1977489

谢谢,我很抱歉,我还在学Django。你知道吗

相关问题 更多 >