How to avoid repeating URL structures in Django - html

The Django tutorial explains how to create a basic poll app. The templates in the tutorial frequently use hard-coded URL structures—for instance:
<form action="/polls/{{ poll.id }}/vote/" method="post">
And:
Vote again?
What would be the best way to refactor this code to avoid repeating /polls/ throughout the templates?

Use the url template tag https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url

Alternatively, name your urls.
See: https://docs.djangoproject.com/en/dev/topics/http/urls/#naming-url-patterns
In the template, the url would look like:
Vote again?
In the view, the url can be retrieved using the reverse method like:
reverse('poll_url', args=[poll.id])

Related

Laravel best way to form an HTML form and token

Hi I am working with Laravel and I have a question about the . Back in time we used to write something like the following when we want to submit data.
<form action="insert.php" method="POST"> </form>
Now, I have seen in Youtube videos and here in stackoverflow many code snips where we use the following:
<form action="{{URL::to('/insert')}}" method="POST">
I would like to know what is the difference? In the second way /insert is pointing to a file or a controller? The first one is wrong? Or it is just an alternative old fashion way?
Also I have seen two ways of inserting token. Which is the best? What are the differences? Both work the same? What will happen if I do not insert a token?
{{csrf_field()}}
#csrf
Thank you for your time!
Well, when you do this:
<form action="insert.php" method="POST"> </form>
What you are doing is telling the html markup explicitly the relative path to post the data, in your case index.php
Say, you were on this url in your app: http://myapp.com/some-page and you were click the submit button, what will happen is that it will post the data to this relative url: http://myapp.com/some-page/index.php
Now, the reason why we use the URL helper facade is to make urls relative to the application's url.
For example, if you've defined in your application config (or .env) that the APP_URL is equal to something like http://myapp.com then when you use this: URL::to('/insert') it will output the following url: http://myapp.com/insert - regardless of which url you are on.
Hope this makes sense. As for this:
{{csrf_field()}}
#csrf
I believe they achieve the same, they generate an hidden input field with your current csrf token in it.

Is it possible to capture part of the URL as data in Jekyll?

I have a docs collection and I have urls like this:
/docs/latest
/docs/latest/installation/
/docs/latest/quickstart/
/docs/v0.0.27
/docs/v0.0.27/installation/
/docs/v0.0.27/quickstart/
I'd like to be able to identify the latest vs v0.0.27 part without having to use frontmatter on each page to specify it. Is this possible?

Include html pages with Google Closure

I'm working with Google Closure. I'm trying to include some html files in another one. Just like A.html import B.html and C.html, but actually, I don't get how to do that.
Can anyone could give some orientation please?
Thx in advance.
As far as I know you cant "include" html pages like that. The options you got is:
1: use ajax to fetch content
http://docs.closure-library.googlecode.com/git/closure_goog_net_xhrio.js.html
http://www.googleclosure.com/google-closure-ajax/
2: Google closure templates
https://developers.google.com/closure/templates/?csw=1
3: Use a serverside language like php to include your file.
http://www.php.net/manual/en/function.include-once.php
I really don't understand.
1) Have you HTML in JS and u don't know how to join it?
try goog.dom.appendChild(parent, child)
2) You don't know how to get it into JS?
You have to send it from server, or If I were in your shoes... use soy templates

How do you post to a secure service from unsecure page without using fully qualified domain name?

I would like to post have a form that sits on a non secure page without using the fully qualified domain name.
http://www.domain.com/page.aspx
post to a secure service on the same domain
https://www.domain.com/service.aspx
I am currently doing this which works.
<form action="https://www.domain.com/service.aspx" id="formId" method="POST">
The main issue is that we have qa versions of the site
http://qa.domain.com/page.aspx
https://qa.domain.com/service.aspx
the form here looks like
<form action="https://qa.domain.com/service.aspx" id="formId" method="POST">
and there is some publishing issues because when we publish from qa to prod we have to manually update the domain name.
What I'd like to do is somehow point to
<form action="/service.aspx" id="formId" method="POST">
but make the form use the prefix https. We can dynamically write the URL in the form, but I was looking for a way to do it with HTML.
Thanks
You can't do this with URL syntax. You must always restate anything to the right of the component of a URL that you change when constructing relative URLs.
You could generate the URLs programmatically (preferably with server side code).
Note, that as per my comment on the question, you shouldn't do this.

Rails: Best practice to generate custom HTML in Model?

I let users embed videos from Youtube, Google, Vimeo etc. I thought about the best and most secure approach (I don't want them to be able to include any flash and I also want to restrict the Videosites to exclude free porn websites etc.).
So I thought the best and easiest thing would be to let the user just copy&paste the URL of the video into a text-field, store it in a ExternalVideo Model and then just generate the needed HTML to embed the video.
So my ExternalVideo Model has a function called "embed_html" which should return the proper HTML.
Of course I could do something like this:
def embed_html
# just a very short example to make my point
"<embed src='#{#video_source}'>"
end
But I think that's bad practice and very unreadable.
My Question: Is there a tool / Gem / Built-in function I can use to generate custom HTML, something like the View Helpers (link_to, image_tag, etc)?
Thanks for your help!
I would do the following
def embed_element(external_video)
content_tag(:embed, '', :src => external_video.video_source)
end
You should probably check the docs for more information on the content tag method.
Also note that the content_tag() method will insert a closing tag. Something you seem to be forgetting...
You almost answered your question. Use helper method:
def embed_html url
"<embed src='#{url}'>"
end
And use it in view:
<%= embed_html #video_source %>
Why don't you just write a view helper method that accepts a ExternalVideo model instance, asks it for its video URL and then returns the HTML?