简体   繁体   English

Django-以用户形式输入并在输入的特定部分创建链接?

[英]Django - take users form input and create a link in a certain part of the input?

This is my models.py: 这是我的models.py:

class Post(models.Model):
    actualPost = models.CharField(max_length=200)

And this is my forms.py: 这是我的forms.py:

class PostForm(forms.ModelForm):
    class Meta:
    model = Post
    fields = ['actualPost']
    widgets = { 'actualPost' : forms.Textarea(attrs={'maxlength':200}) }

This is my views.py: 这是我的views.py:

def createPostView(request):
    if request.method=='POST':
    form = PostForm(request.POST)
    if form.is_valid():
            stringPost = form.cleaned_data['actualPost'] #I'm assuming stringPost will now be the string version of the users input
            findLinks(stringPost)
            newPost = Post(actualPost = form.cleaned_data['actualPost'])

So basically in the view, after getting the string version of the users input / post, I plan on sending that string to a function called findLinks. 因此,基本上在视图中,在获得用户输入/发布的字符串版本之后,我计划将该字符串发送至名为findLinks的函数。 This is the findLinks function: 这是findLinks函数:

def findLinks(postString):
    word = ''
    totalLength = len(postString)
    for i in postString:
        if i=='@': #basically, I want anything after an '@' sign to be a link.. 
                   #so if the post is "Go to @www.google.com to search for anything"
                   #I want www.google.com to be a link 
            indexOfSign = postString.index(i)
            while ((indexOfSign != totalLength-1) and (postString[indexOfSign+1] != '')):
                 word += htmlString[indexOfSign+1]
                 indexOfSign += 1
    return word

The function does return the word after the '@' sign. 该函数的确会在“ @”符号后返回单词。 So now that it returns the word after the @ sign, how I do safely make that word a link so that when I put the post in a template, then when I view the template, the word after the @ sign will appear as a link? 因此,既然它返回了@符号后的单词,那么我如何安全地将该单词设置为链接,以便当我将帖子放入模板中时,然后当我查看模板时,@符号后的单词将显示为链接。 ?

One possible way I was thinking was, in the python function, basically return the string version of the post except with the word after the @ sign wrapped around with "" and "", but this will not work because for safety reasons, Django interprets user input as a raw string rather than code, correct? 我想过的一种可能方法是,在python函数中,基本上返回该帖子的字符串版本,除了@符号后的单词外用“”和“”包裹,但这无法正常工作,因为出于安全原因,Django解释了用户输入为原始字符串而不是代码,对吗?

I don't want to force Django to interpret Post's as code rather than raw string because that will cause security issues. 我不想强迫Django将Post解释为代码而不是原始字符串,因为这会导致安全问题。 Is there any way for me to safely take the users input and save it to the database and in the template, make all words which come after the '@' sign a link? 我有什么办法可以安全地接受用户输入并将其保存到数据库中,并在模板中使“ @”后的所有单词都链接起来?

Declare a class method like this: 声明这样的类方法:

class Post(models.Model):
    actualPost = models.CharField(max_length=200)

    def get_link(self):
        if self.actualPost and '@' in self.actualPost:
            return self.actualPost.split('@', 1)[1]
        return None

and in the template 并在模板中

{% for post in post_list %}
    {% if post.get_link %}
        <a href="/{{ post.get_link }}/">Blah </a>
    {% endif %}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM