How to create a url that has a string parameter using Django - django

I am looking to show one of two modals that are on one template file. The modals are displaying correctly based on a url string parameter (status=1 or status=0). I want to create a url in the form example.com/page?status=1
I have two buttons on another page with code as below:
MyDisplayText
How can I add my status=1 string to the url being generated?

You just add the text:
MyDisplayText
if your value is coming from the template context, then output the variable:
MyDisplayText

Related

How to get current url or input parameters in web.py template

I have a web page designed with web.py. I use web.template.render() and an HTML template file to draw a table in this web page.
I pass a list of items to this template, and would like to show the data in different pages using GET method and offset and count parameters (i.e. http://mywebsite/records?offset=10&count=15).
I am looking for a way to get the input values of offset and count and also the current url in the HTML template file, so I would be able to put links to next page and previous page.
Templates get data either passed into them (e.g., render.home(a, b, c)) or as a global (explicitly passed in as a dict via globals parameter when you define your renderer).
Your example is easier to do by getting the information in python, and passing it into the template. This allows you to error check the values, provide defaults, etc.
So, have your GET() extract the proper information and pass it to the template:
class index(object):
def GET(self):
my_render.index(url=web.ctx.fullpath,
offset=web.input().offset,
count=web.input().count())
==== index.html ====
$def with(url, offset, count)
<table>
<tr><td>URL is: $url</td>
<td>OFFSET is: $offset</td>
<td>COUNT is: $count</td></tr>
</table>

how to pass controls value to url?

I have a nintex form "A", that has a lot of controls.
I used a label control and in edit source i used html below to link to another form "B".
<a onclick="javascript:SP.UI.ModalDialog.showModalDialog({ url:'http://servername/.../newform.aspx' }); return false;" href="#">my link</a>
that works fine.
But I want to show one of form "A"s controls value in form "B".so i want to pass control value with this url to that form using querystring.Is it possible?
i mean use for example ?project=contolname at the end of url.
you can just keep it the way you have it and append ?project=contolname to the end of the URL and it will go through fine.

django url parse formatted url

I'm in the design stages of a single page web app, and would like to make it so that a user can click on a formatted URL and the data requests will load in the page.
For example, a url of http://www.mysite.com/?category=some_cat will trigger the Category view with the relevant data.
My intention is to parse the URL, gather the data, then pass it to the index.html template for rendering on page load. Once the page has been loaded, a Javascript trigger setting will trigger the appropriate button to load the client view.
However, I'm having an issue setting up the URL parser, as the following settings are not matching the example url above.
from app.views import app_views, photo_views, user_views, admin_views
urlpatterns = patterns("",
url(r'^/(?P<category>\d+)/$', app_views.index)
)
You're confusing between sending information through your urls with GET and formatting you urls with arguments for the view functions. Say I am visiting a site called http://www.mysite.com/ and the page has a form that looks like this:
<form>
<input type='text' name='category' id='category'></input>
<button type='submit'>Send!</button>
</form>
upon clicking, the url will automatically change to http://www.mysite.com/?category=<value of input>. The ? marks that everything afterwards should be treated as GET data, with the syntax of <id>=<value>. You can then access them like so:
def response(request):
category = request.GET['category']
formatting urls is different, because it means looking for patterns that are part of the url. i.e. a pattern that looks like r'^/(?P<category>\d+)/$' will look for this: http://www.mysite.com/<category>/ and it will send it to the request in your views as an additional argument like so:
def response(request, category):
...
The regex is used to define how you recognize that part of the url. For example, the \d+ you're using means that category needs to be a number. You can search how to define different types of patterns according to your needs
Note that with GET you are sending the data to the same view function that rendered the page you are currently visiting, while using a different url means you tell it where to go through your urls.py (usually a different function). Does that make things a bit clearer?

How to create a WordPress like URL naming convention in Django?

I'm a newbie in Django and in WordPress if you create a Post called "hello world" then the URL by default will be like
wordpress.com/2012/07/05/hello-world/
and if you create another post with the same name it will be
wordpress.com/2012/07/05/hello-world-2/
I want to achieve the same in Django and I was thinking to create a sample urlconf like this
(r'^articles/(\d{4})/(\d{2})/(?P<name>\w+)', 'article.views.article_detail')
and in the views break down the name and iterate through all the items and match the name.
But the problem with will be that I won't be able to reference posts dynamically. For e.g. if I was to link the a hello world post I would need to find out how many posts with the same name exist already and then append the additional number to it which is inefficient.
So what's the best way to do this in Django?
See the documentation for Django's {{ url }} template tag. It lets you pass it a view name and parameters, and automatically generates the correct URL for you.
You can take care of appending numbers to each post's name in the function that generates its slug - you could have a look at django-autoslug

Django url and request GET in template

I am using "url" tag in my template and everything works fine, except I cant capture anything thats behind it. Since I have multiple filters on that page, that are kept via GET request in the url, I need to be able to apend them to it. What happens is, that when I select one filter url will change to some/url/?f=1, then when I select another filter previous filter will get overriden, since the url is just some/url without request.
Here is a piece from urls.py:
url('^products/$', products_list, name = 'products_list'),
Is there anyway to modify it so the url tag will capture the GET request? Or do I need to create a filter which will add it there?
Any help is appreciated
Regards
There is no way to generate a query string using the url tag. If you need to add a query string to the output then do it manually, e.g. {% url foo bar %}?var={{ val|urlencode }}.