Django: How To Pass Only Selected Arguments Through URL - python

I am trying to create a search page where buttons can be clicked which will filter the posts like in this page [Splice Sounds][2] (i think you need an account to view this so ill add screenshots).
To do this i think i need to pass a list so that i can filter by that list but i can't find a way to do this.
having a GET form for each genre (which is being created by a for loop) would allow me to filter by one genre at a time but i want to filter by multple genres at once so that won't work
in the site that i linked to: they pass the genres/tags into the url so how could i do this in django?
Also: i could make seperate url paths and link to those but then i would have to do this for every combination of genres/tags which would be too much so i can't do that.
the link shows a site which passes tags through url like this https://splice.com/sounds/search?sound_type=sample&tag=drums,kicks
here is some relevant code:
this is how i want to filter which is why i need to pass a list of args
for arg in args:
Posts = Posts.filter(genres=arg)
urls
urlpatterns = [
path('', views.find, name='find'),
path('searchgenres=<genres_selected>', views.find_search, name='find_search'),
]
EDIT: I have tried this many ways such as using ajax but i couldn't get that to work well
EDIT 2: i have changed the question to How To Pass Only Selected Arguments Through URL

To pass a list into a request you could:
Use html checkboxes and in views aggregate them into a list
Use a single textbox and parse in views
If you obtain the request as a list, you could use Post.objects.filter(genre__in=genres).
It might also be helpful to know that Django allows for complex lookups with Q objects from django.db.models import Q. The | character represents OR. This allows complex filtering. For instance:
Posts.objects.filter(Q(genre='Pop') | Q(genre='Rock') | Q(genre='Jazz'))

Related

how to create link in django for substring

I am struggling to understand how to create a link in django's templates.
in my views.py file I have a list of lists (so a table). One of the fields may or may not contain links.
Views.py
#tableToView is a pd.DataFrame()
actualTable = []
for i in range(tableToView.shape[0]):
temp = tableToView.iloc[i]
actualTable.append(dict(temp))
#Returning the 'actualTable' list of lists to be printed in table format in the template
return render(response, "manual/manualinputs.html", {'defaultDates':defaultDates, 'prevFilterIn':prevFilterIn, 'actualTable':actualTable, 'DbFailure':DbFailure})
so imagine my actualtable has a field 'News' that may or may not contain a link, e.g.:
'hello there, go to https://www.google.com/'
How do I manage the fact that I want in my template to have the same string printed out, but with the address actually being a link?
I will also extend this to non-addresses (say twitter hashtags by parsing text).
Should I act on views or templates?
I did try to go the views way:
I substitute the
'hello there, go to https://www.google.com/'
with
'hello there, go to https://www.google.com/'
But I get the actual tags printed out, which is not what I want...
Any ideas?
If you change it on the view, you need to print it with |safe on the template.
So it would be
{{table.column|safe}}, this way your link will be a link and not a string

ı wanna post two id from .html to views

html file ı wanna take two id to views.py can ı get it ?
path('yorum_sil/<int:id>',views.yorum_sil,name="yorum_sil")
I want to do this code like the below
path('yorum_sil/<int:comment_id> and <int:post_id>',views.yorum_sil,name="yorum_sil")
That is possible, although you should not use spaces like that. You can for example use a slash like:
path('yorum_sil/<int:comment_id>/<int:post_id>',views.yorum_sil,name="yorum_sil")
Your view function (here yorum_sil) then of course needs to accept the two parameters, like:
# app/views.py
def yorum_sil(request, comment_id, post_id):
# ...
return ...
and if you perform a reverse lookup, you need to pass the two parameters. For example in a template like:
some_link
If I translated yorum sil correctly it means delete comments, note that typically GET requests should not have side effects. In order to delete/create/... an entity, you should use a POST request.

How to use loaded data to add a new value in an ItemLoader?

I have started a scraping project, and I have a small problem with ItemLoader.
Suppose I have some ItemLoader in a scraper:
l = ScraperProductLoader(item=ScraperProduct(), selector=node)
l.add_xpath('sku', 'id/text()')
I would like to add a URL to the item loader based on the sku I have provided:
l.add_value('url', '?????')
...However, based on the documentation, I don't see a clear way to do this.
Options I have considered:
Input processor: Add a string, and pass the sku as the context somehow
Handle separately: Create the URL without using the item loader
How can I use loaded data to add a new value in an ItemLoader?
You can use get_output_value() method:
get_output_value(field_name)
Return the collected values parsed using
the output processor, for the given field. This method doesn’t
populate or modify the item at all.
l.add_value('url', 'http://domain.com/' + l.get_output_value('scu'))

How to have multiple get params in an URL?

I'm using Django to create a website, and in one portion I need two get requests in the url - one called "search", and one called "page". I've tried the following -
return HttpResponseRedirect('/explore/?search=test/?page=1')
However, '/?page=1' is being included as part of the search, and this is messing it up. Any way to keep them as two different gets, or will I have to fuse them into one?
Do it like this:
return HttpResponseRedirect('/explore/?search=test&page=1')
and in view you can get both parameters as:
search = request.GET.get('search')
page = request.GET.get('page')

whoosh MultifieldParser field search or query parser concatenation

I'm trying to use whoosh to add search functionality to my blogapp on appengine but I don't understand some stuff.
The blogentries are indexed with title, content and status fields.
I would like to have different type of results on the public page then on the admin page but without the need to have multiple indexes.
On the frontpage I want visitors to be able to search on visible entries only on the title and content fields and in the admin I want to search also on draft entries.
Can i concatenate searches using QueryParser so I can search on multiple fields?
How could I filter on status:visible with MultifieldParser?
EDIT
didn't test it yet but i got an answer on the whoosh mailing list:
# Create a parser that will search in title and content
qp = qparser.MultifieldParser(["title", "content"], ix.schema)
# Parse the user query
q = qp.parse(user_query_string)
# If request is not admin, filter on status:visible
filterq = query.Term("status", u"visible") if not is_admin else None
# Get search results
results = searcher.search(q, filter=filterq)
I know this is not strictly an answer but Google added a full text search api similar to whoosh. Perhaps you should try it.
https://developers.google.com/appengine/docs/python/search/overview

Categories

Resources