Using an html link to input value into django view - python

Pretty new to Django still. I have a map that the user will click on a country. I want that link to add the country name to:
URL:
url(r'^country/(?P<name>\w+)' , 'wiki.views.country',
name = 'wiki_country'),
Then the country view will open. I want to do it this way so that I can create a dynamic country view that can populate based on the name of the country, rather than individual pages served for each country. The view looks like this so far:
def country(request, name):
country = Country.objects.get(name=name)
return render_to_response("wiki/country.html") , {'country' : country})
I don't know how to take the map, or even just a text hyperlink and pass the name into the URL.

use name url patterns.
Add named urls with argument in 'href' in templates.
{% country_object.name %}
'country_object.name' is your input argument for country function.
Django Docs reference of named urls

Related

Displaying the content on a different html based on the primary key

I am using Django to create a site. I have Table called notice as below
Models.py
class Notice(models.Model):
Notice_No=models.AutoField(primary_key=True)
Content=models.TextField(max_length=5000, help_text="Enter Owner Name")
I am using a for loop to display the all these fields on my template page as
Notice No, Content, Date of issue, Date of expiry. I have provided a hyperlink to the all the content values which take it to another HTML play with a proper notice formal of a CHS. Now what I wanna do is if I click on let's say notice of notice no-1. I only want to display the content of that notice on the next page. If I click on notice _no 2, it should only display the contents of that notice. I'm new to python so not sure how to do this. How do I go about this?
Notice.html is the page that displays the table. Noticesoc.html display is where the content should be displayed.
views.py
def notices(request):
Notice_all=Notice.objects.all()[:50]
return render(
request,
'notices.html',
context={'Notice_all':Notice_all}
)
def noticesoc(request):
Notice_all=Notice.objects.all()
return render(
request,
'noticesoc.html',
context={'Notice_all':Notice_all}
)
Send the primary key of the data you want to see in detail.
<td style="color:white; font-family:helvetica; font-size:15px;">
<a class="nav-link" href="{% url 'noticesoc' Notice.pk %}">
Click here to view the contents</a>
</td>
url( r'^noticesoc/(?P<pk>\d+)/$', 'noticesoc')
Then in the view. Use .get to get the information Notice and then render it.
Ex:
def noticesoc(request, pk):
Notice=Notice.objects.get(id=pk)
return render(
request,
'noticesoc.html',
context={'Notice_all':Notice}
)
in reference to this ....
Still doesn't work. Error=local variable 'Notice' referenced before assignment. Can you see my url.py(previous comment) and see if it's correct
you will notice that the model name and the function name are similar hence instead of django differentiating the two it assumes them as similar with the first code to run as the model then the view function
try to use another name for your view function and call the name different and unique from the model name for instance
def noticesoc(request, pk):
Notice=Notice.objects.get(id=pk)
return render(
request,
'noticesoc.html',
context={'Notice_all':Notice}
)
use this instead
def noticesoc(request, pk):
note=Notice.objects.get(id=pk)**
return render(request,'noticesoc.html',context={'note':note}
emphasized text

Passing variables from Jinja to database

I'm trying to figure out a way to pass a dynamic URL with webpy that will render a Jinja template with information about the information that was passed. Specifically I have a item database that should be able to take whatever item ID is in the URL and render a template with further information about that item.
To simply the problem, I've hardcoded the value 1043374545 for demonstrations purposes, but I'm hoping that this line will be come dynamic once this initial problem is solved.
urls = ('/1043374545', 'view_item')
class view_item:
def GET(self, itemID):
item_info = sqlitedb.detailInfo(request)
return render_template('view_item.html', item = item_info)
As of now I isolated the issue to having to do with something related to passing the value 1043374545 into the view_item function. Any thoughts on how I can pass a dynamic number within a URL into view_item?
simply put:
urls = (('/([0-9]*)', 'view_item'),)
[0-9]* will tell webpy to only accept numbers after the "/".
Then you can use that value in your GET function, it will be under itemID as you specified in your GET parameter.
class view_item:
def GET(self, itemID):
item_info = sqlitedb.detailInfo(itemID)
return render_template('view_item.html', item = item_info)
check this link for more details:
http://webpy.org/cookbook/url_handling

How to create page hierarchy in Odoo?

In Odoo, pages are created with url /page/page-name and basically it creates a template with id website.page-name and going to /page/website.page-name redirects to /page/page-name.
I need to create a page category with subpages and have the url look like /category-name/subpage-name (e.g. example-website.com/films/sherlock). Is this possible without having to define a web controller that renders the page? Is it possible to map /page/category-name.subpage-name to /category-name/subpage-name?

django urls named groups

I want to match any url with a numeric id (various length) at the end and pass that id to a view function.
My urls are in this form:
/event/dash-separated-strings-2014-12-16-342614/
The id is 342614. The content before the date is also various.
Here is my url config:
url(r'^event/(*.-\d{4}-\d{2}-\d{2}-)(?P<event_id>\d*)/$', view_event , name='my_view_event')
The problem is that the full url is passed to my view function. What i want is the named group only. What is wrong with my config?
Try this:
url(r'^event/[\w\-]+-\d{4}-\d{2}-\d{2}-(?P<event_id>\d+)/$', view_event , name='my_view_event')

Passing a variable from template in Django

I want to create a generic country view that populates data based on the what country i clicked on a map. The current URL looks like:
# Country View URL
url(r'^country/' , 'wiki.views.country',
name = 'wiki_country'),
And the view is:
def country(request):
return render_to_response("wiki/country.html")
This is fine if I want a separate page for each country. I was reading elsewhere that there is no simple way to get a variable from a template in Django. What I would want is for the link in the template not only have the URL, but also a value ("Country Name") that then allows me to dynamically populate a single countryView template.
For specific country which will take id and name of country you can define the URL pattern as:
url(r'^country/(?P<id>\d+)/(?P<name>\w+)' , 'wiki.views.country', name='wiki_country'),
The view will be:
def country(request, id, name):
country = Country.objects.get(id=id, name=name)
return render_to_response("wiki/country.html", {'country': country})
In listing template you can have a link as:
{% for country in countries %}
{{ country.name }}
{% endfor %}
Hope this will lead you somewhere.

Categories