How do I render a URLField nicely in a template
up vote
1
down vote
favorite
The problem
I have a model for a league like
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
where league_url
can be a link to an external league page (i.e. not my site).
Now, let's say a user creates a league and sets league_url to "pelicanpark.com/leagues/27". When I render this in a template, league.league_url
it shows http://www.pelicanpark.com/leagues/27/
which takes up a lot of real estate.
So, I need to slim it down. My thought is to render something like
<a href="http://www.pelicanpark.com/leagues/27/">pelicanpark.com/...</a>
but this has some obvious complexities. Does anyone have a good solution for this?
django django-templates
add a comment |
up vote
1
down vote
favorite
The problem
I have a model for a league like
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
where league_url
can be a link to an external league page (i.e. not my site).
Now, let's say a user creates a league and sets league_url to "pelicanpark.com/leagues/27". When I render this in a template, league.league_url
it shows http://www.pelicanpark.com/leagues/27/
which takes up a lot of real estate.
So, I need to slim it down. My thought is to render something like
<a href="http://www.pelicanpark.com/leagues/27/">pelicanpark.com/...</a>
but this has some obvious complexities. Does anyone have a good solution for this?
django django-templates
by ... do you mean you wantpelicanpark.com/...
orpelicanpark.com/leagues/27/
?
– Ben
Mar 14 '15 at 22:12
Good question. I meanpelicanpark.com/...
– Ben
Mar 14 '15 at 22:21
Can you expand more on the complications by doing the following in the template?<a href=" league.league_url ">pelicanpark.com/...</a>
wherein league is the user's league instance passed to the template. That seem like it would be the easiest?
– Ben
Mar 14 '15 at 22:31
@Ben pelicanpark.com is just an example url. In reality the league_url could be anything. E.g. if the url was "myleaguesite.com/leagues/123" I'd want to display<a href=" league.league_url ">myleaguesite.com/...</a>
. In other words, the "domain.com/..." should be rendered dynamically depending on the actual league_url.
– Ben
Mar 15 '15 at 0:23
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
The problem
I have a model for a league like
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
where league_url
can be a link to an external league page (i.e. not my site).
Now, let's say a user creates a league and sets league_url to "pelicanpark.com/leagues/27". When I render this in a template, league.league_url
it shows http://www.pelicanpark.com/leagues/27/
which takes up a lot of real estate.
So, I need to slim it down. My thought is to render something like
<a href="http://www.pelicanpark.com/leagues/27/">pelicanpark.com/...</a>
but this has some obvious complexities. Does anyone have a good solution for this?
django django-templates
The problem
I have a model for a league like
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
where league_url
can be a link to an external league page (i.e. not my site).
Now, let's say a user creates a league and sets league_url to "pelicanpark.com/leagues/27". When I render this in a template, league.league_url
it shows http://www.pelicanpark.com/leagues/27/
which takes up a lot of real estate.
So, I need to slim it down. My thought is to render something like
<a href="http://www.pelicanpark.com/leagues/27/">pelicanpark.com/...</a>
but this has some obvious complexities. Does anyone have a good solution for this?
django django-templates
django django-templates
asked Mar 14 '15 at 20:34
Ben
5,8961259117
5,8961259117
by ... do you mean you wantpelicanpark.com/...
orpelicanpark.com/leagues/27/
?
– Ben
Mar 14 '15 at 22:12
Good question. I meanpelicanpark.com/...
– Ben
Mar 14 '15 at 22:21
Can you expand more on the complications by doing the following in the template?<a href=" league.league_url ">pelicanpark.com/...</a>
wherein league is the user's league instance passed to the template. That seem like it would be the easiest?
– Ben
Mar 14 '15 at 22:31
@Ben pelicanpark.com is just an example url. In reality the league_url could be anything. E.g. if the url was "myleaguesite.com/leagues/123" I'd want to display<a href=" league.league_url ">myleaguesite.com/...</a>
. In other words, the "domain.com/..." should be rendered dynamically depending on the actual league_url.
– Ben
Mar 15 '15 at 0:23
add a comment |
by ... do you mean you wantpelicanpark.com/...
orpelicanpark.com/leagues/27/
?
– Ben
Mar 14 '15 at 22:12
Good question. I meanpelicanpark.com/...
– Ben
Mar 14 '15 at 22:21
Can you expand more on the complications by doing the following in the template?<a href=" league.league_url ">pelicanpark.com/...</a>
wherein league is the user's league instance passed to the template. That seem like it would be the easiest?
– Ben
Mar 14 '15 at 22:31
@Ben pelicanpark.com is just an example url. In reality the league_url could be anything. E.g. if the url was "myleaguesite.com/leagues/123" I'd want to display<a href=" league.league_url ">myleaguesite.com/...</a>
. In other words, the "domain.com/..." should be rendered dynamically depending on the actual league_url.
– Ben
Mar 15 '15 at 0:23
by ... do you mean you want
pelicanpark.com/...
or pelicanpark.com/leagues/27/
?– Ben
Mar 14 '15 at 22:12
by ... do you mean you want
pelicanpark.com/...
or pelicanpark.com/leagues/27/
?– Ben
Mar 14 '15 at 22:12
Good question. I mean
pelicanpark.com/...
– Ben
Mar 14 '15 at 22:21
Good question. I mean
pelicanpark.com/...
– Ben
Mar 14 '15 at 22:21
Can you expand more on the complications by doing the following in the template?
<a href=" league.league_url ">pelicanpark.com/...</a>
wherein league is the user's league instance passed to the template. That seem like it would be the easiest?– Ben
Mar 14 '15 at 22:31
Can you expand more on the complications by doing the following in the template?
<a href=" league.league_url ">pelicanpark.com/...</a>
wherein league is the user's league instance passed to the template. That seem like it would be the easiest?– Ben
Mar 14 '15 at 22:31
@Ben pelicanpark.com is just an example url. In reality the league_url could be anything. E.g. if the url was "myleaguesite.com/leagues/123" I'd want to display
<a href=" league.league_url ">myleaguesite.com/...</a>
. In other words, the "domain.com/..." should be rendered dynamically depending on the actual league_url.– Ben
Mar 15 '15 at 0:23
@Ben pelicanpark.com is just an example url. In reality the league_url could be anything. E.g. if the url was "myleaguesite.com/leagues/123" I'd want to display
<a href=" league.league_url ">myleaguesite.com/...</a>
. In other words, the "domain.com/..." should be rendered dynamically depending on the actual league_url.– Ben
Mar 15 '15 at 0:23
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
There's probably some fancy way of using regex for this kind of thing, but that's above my head. Luckly it looks like there's urlparse for python. To implement it in your example:
from urlparse import urlparse
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
def url_text(self):
parsed_url = urlparse(self.league_url)
return parsed_url.hostname.replace("www.", "") + "/..."
Then in template:
<a href=" league.league_url "> league.url_text </a>
I would refrain from rendering the html in the backend. I've always been told there's security risks in doing that.
add a comment |
up vote
0
down vote
You can create a field with MethodSerializer
class League(models.Model):
league_name = models.CharField(max_length=60)
nice_league_url = serializers.SerializerMethodField()
def get_league_url(self,obj):
final_url = "<a href="+obj.league_url+">pelicanpark.com/...</a>"
return final_url
add a comment |
up vote
0
down vote
The above answers are spot on. I just ran into the same problem and found some changes. For Python3 and higher, use:
from urllib.parse import urlparse
instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29053875%2fhow-do-i-render-a-urlfield-nicely-in-a-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
There's probably some fancy way of using regex for this kind of thing, but that's above my head. Luckly it looks like there's urlparse for python. To implement it in your example:
from urlparse import urlparse
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
def url_text(self):
parsed_url = urlparse(self.league_url)
return parsed_url.hostname.replace("www.", "") + "/..."
Then in template:
<a href=" league.league_url "> league.url_text </a>
I would refrain from rendering the html in the backend. I've always been told there's security risks in doing that.
add a comment |
up vote
1
down vote
There's probably some fancy way of using regex for this kind of thing, but that's above my head. Luckly it looks like there's urlparse for python. To implement it in your example:
from urlparse import urlparse
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
def url_text(self):
parsed_url = urlparse(self.league_url)
return parsed_url.hostname.replace("www.", "") + "/..."
Then in template:
<a href=" league.league_url "> league.url_text </a>
I would refrain from rendering the html in the backend. I've always been told there's security risks in doing that.
add a comment |
up vote
1
down vote
up vote
1
down vote
There's probably some fancy way of using regex for this kind of thing, but that's above my head. Luckly it looks like there's urlparse for python. To implement it in your example:
from urlparse import urlparse
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
def url_text(self):
parsed_url = urlparse(self.league_url)
return parsed_url.hostname.replace("www.", "") + "/..."
Then in template:
<a href=" league.league_url "> league.url_text </a>
I would refrain from rendering the html in the backend. I've always been told there's security risks in doing that.
There's probably some fancy way of using regex for this kind of thing, but that's above my head. Luckly it looks like there's urlparse for python. To implement it in your example:
from urlparse import urlparse
class League(models.Model):
league_name = models.CharField(max_length=60)
league_url = models.URLField(null=True, blank=True, default='')
def url_text(self):
parsed_url = urlparse(self.league_url)
return parsed_url.hostname.replace("www.", "") + "/..."
Then in template:
<a href=" league.league_url "> league.url_text </a>
I would refrain from rendering the html in the backend. I've always been told there's security risks in doing that.
edited Mar 17 '15 at 18:26
answered Mar 15 '15 at 0:49
Ben
3,37822144
3,37822144
add a comment |
add a comment |
up vote
0
down vote
You can create a field with MethodSerializer
class League(models.Model):
league_name = models.CharField(max_length=60)
nice_league_url = serializers.SerializerMethodField()
def get_league_url(self,obj):
final_url = "<a href="+obj.league_url+">pelicanpark.com/...</a>"
return final_url
add a comment |
up vote
0
down vote
You can create a field with MethodSerializer
class League(models.Model):
league_name = models.CharField(max_length=60)
nice_league_url = serializers.SerializerMethodField()
def get_league_url(self,obj):
final_url = "<a href="+obj.league_url+">pelicanpark.com/...</a>"
return final_url
add a comment |
up vote
0
down vote
up vote
0
down vote
You can create a field with MethodSerializer
class League(models.Model):
league_name = models.CharField(max_length=60)
nice_league_url = serializers.SerializerMethodField()
def get_league_url(self,obj):
final_url = "<a href="+obj.league_url+">pelicanpark.com/...</a>"
return final_url
You can create a field with MethodSerializer
class League(models.Model):
league_name = models.CharField(max_length=60)
nice_league_url = serializers.SerializerMethodField()
def get_league_url(self,obj):
final_url = "<a href="+obj.league_url+">pelicanpark.com/...</a>"
return final_url
edited Mar 14 '15 at 20:45
answered Mar 14 '15 at 20:38
levi
14k24553
14k24553
add a comment |
add a comment |
up vote
0
down vote
The above answers are spot on. I just ran into the same problem and found some changes. For Python3 and higher, use:
from urllib.parse import urlparse
instead.
add a comment |
up vote
0
down vote
The above answers are spot on. I just ran into the same problem and found some changes. For Python3 and higher, use:
from urllib.parse import urlparse
instead.
add a comment |
up vote
0
down vote
up vote
0
down vote
The above answers are spot on. I just ran into the same problem and found some changes. For Python3 and higher, use:
from urllib.parse import urlparse
instead.
The above answers are spot on. I just ran into the same problem and found some changes. For Python3 and higher, use:
from urllib.parse import urlparse
instead.
edited Nov 11 at 2:01
Stephen Rauch
27.6k153256
27.6k153256
answered Nov 11 at 1:43
Shrijana Ghimire
212
212
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29053875%2fhow-do-i-render-a-urlfield-nicely-in-a-template%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
by ... do you mean you want
pelicanpark.com/...
orpelicanpark.com/leagues/27/
?– Ben
Mar 14 '15 at 22:12
Good question. I mean
pelicanpark.com/...
– Ben
Mar 14 '15 at 22:21
Can you expand more on the complications by doing the following in the template?
<a href=" league.league_url ">pelicanpark.com/...</a>
wherein league is the user's league instance passed to the template. That seem like it would be the easiest?– Ben
Mar 14 '15 at 22:31
@Ben pelicanpark.com is just an example url. In reality the league_url could be anything. E.g. if the url was "myleaguesite.com/leagues/123" I'd want to display
<a href=" league.league_url ">myleaguesite.com/...</a>
. In other words, the "domain.com/..." should be rendered dynamically depending on the actual league_url.– Ben
Mar 15 '15 at 0:23