I have a few checkboxes with common name and individual variables (ID).
How can I in python read them as list?
Now I'm using
checkbox= request.POST["common_name"]
It isn't work properly, checkbox variable store only the last checked box instead of any list or something.
If you were using WebOB, request.POST.getall('common_name') would give you a list of all the POST variables with the name 'common_name'. See the WebOB docs for more.
But you aren't - you're using Django. See the QueryDict docs for several ways to do this - request.POST.getlist('common_name') is one way to do it.
checkbox = request.POST.getlist("common_name")
And if you want to select objects (say Contact objects) based upon the getlist list, you can do this:
selected_ids = request.POST.getlist('_selected_for_action')
object_list = Contact.objects.filter(pk__in=selected_ids)
Related
I'm completely new at programming. As an administrator, I need to update items in an ArcGIS Online group to be shared with everyone. I'm building the script in Jupyter notebook. I found the below link on arcig python api page, and I've been able to get the items from the group, but I don't know how to loop and update the share of each item in the group. https://developers.arcgis.com/python/guide/accessing-and-managing-groups/
You could do a content search for the items and you can use the share method. This is not tested but should work.
from arcgis.gis import GIS
ago_gis = GIS()
webmaps = ago_gis.content.search("My Web Map", item_type="Web Map")
for x in webmaps:
x.share(everyone=True)
I was looking to do something similar but found that I had to use the share method carefully. The share method has default values of False for the 'everyone' and 'org' parameters. If you do not specify them, they default to False or do not share with everyone / do not share with my org.
In my case, I was trying to share an item with a group I made so I used the share method and only passed it the group argument. While it shared it with the intended group, it used the default values of the optional arguments 'everyone' and 'org' which set the items to only be viewed by the 'owner'. This broke any public facing apps and maps which this item was a part of.
In order to modify only the group setting, I needed to call for the item's current sharing settings (using the shared_with property), modify them as needed, and pass them into the share method with the modifications. This allowed me to keep them shared with the public or my org, but also add them to the group I needed them added to.
Here's the code I used in my notebook:
item_sharing_status = item.shared_with
item.share(everyone = item_sharing_status['everyone'],
org = item_sharing_status['org'],
groups = item_sharing_status['groups'] + [backup_group.id])
You can also put the items to share in a list with :
gis.content.share_items(items=[item1, item2, item3], everyone=True, org=True)
link to the doc
I try to filtering django some specific user. I try with list but it not working. Do you have any solution.
list = ['bill gates','elon musk','aamir khan','larry page']
allPosts = Post.objects.filter(author=list)
When I change list filter can work dynamically
You can use __in:
my_list = ['bill gates','elon musk','aamir khan','larry page']
allPosts = Post.objects.filter(author__in=my_list)
Note: Never use python built in functions (Ex: list) as variable names. It would be better to avoid the need for this by choosing a different variable name.
out_links = Link.objects.filter(iweb=iweb_id).order_by('-pub_date')
for link in out_links:
comments = LinkComment.objects.filter(link=link.id)
Filter method creates the list of object, so out_links is a list, right ?
Next, after for loop, I filtering again to find objects in LinkComments class by link id.
The question arises though, shoud I refer to link as it would be the object or rather a list?
I'm not shure about it as long it is django views? link.id or link['id']? My python says [ ], but django does not work.
The out_links is a queryset and in the for loop you can reach all LinkComments by:
for link in out_links:
comments = link.linkcomment_set.all()
Filter creates a QuerySet, as explained in the documentation: https://docs.djangoproject.com/en/dev/ref/models/querysets/#methods-that-return-new-querysets
If you subscript a QuerySet, like comments[n], you get the nth member (just as you would with a list). Where you have an order_by, that is in the order specified by that clause. You cannot query by id using the subscript notation.
When you iterate over the QuerySet, you get the members of the queryset, which are python model objects, and you may treat them just as you do anywhere else in your code.
Filter method creates the list of object, so out_links is a list,
right ?
Wrong. It creates QuerySet object, which also happens to be an iterable.
I'm having some problems in changing the empty label for a form. I've read the documentation https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ModelChoiceField and some pages here on stackoverflow, but I still don't get it. In my models, I have a tuple of countiescounty_choices = (('county1', _('county1')),('county2', _('county2')),('county3', _('county3')))
and the modelcounty = models.CharField(max_length=30, blank=True, default='n/a',choices=county_choices,verbose_name=_('county'), help_text=_('County')) I want to be able to override the 9 '-' empty label with a string in my own choice and to be able to translate it. So I imported everything from models and tried in forms county = forms.ModelChoiceField(queryset=Users.objects.all(),empty_label=_("my_own_choice")) and it does not work. I send it the {{ form.county }} variable in my html, but after my own choice, I get a wierd string 'sdadsada dsadadas' instead of my list of counties. Can you help me with that? Do I need to put the tuple with counties within a queryset? What if I don't want to send it a queryset at all?
Create a model for the counties and syncdb. This way you can manipulate and access them with a queryset, same way you do with Users. i.e. queryset=Counties.objects.all().
Look at the ModelChoiceField documentation for more help.
I'm trying to parse an html form using mechanize. The form itself has an arbitrary number of hidden fields and the field names and id's are randomly generated so I have no obvious way to directly select them. Clearly using a name or id is out, and due to the random number of hidden fields I cannot select them based on the sequence number since this always changes too.
However there are always two TextControl fields right after each other, and then below that is a TextareaControl. These are the 3 fields I need access too, basically I need to parse their names and all is well. I've been looking through the mechanize documentation for the past couple hours and haven't come up with anything that seems to be able to do this, however simple it should seem to be (to me anyway).
I have come up with an alternate solution that involves making a list of the form controls, iterating through it to find the controls that contain the string 'Text' returning a new list of those, and then finally stripping out the name using a regular expression. While this works it seems unnecessary and I'm wondering if there's a more elegant solution. Thanks guys.
edit: Here's what I'm currently doing to extract that info if anyone's curious. I think I'm probably just going to stick with this. It seems unnecessary but it gets the job done and it's nothing intensive so I'm not worried about efficiency or anything.
def formtextFieldParse(browser):
'''Expects a mechanize.Browser object with a form already selected. Parses
through the fields returning a tuple of the name of those fields. There
SHOULD only be 3 fields. 2 text followed by 1 textarea corresponding to
Posting Title, Specific Location, and Posting Description'''
import re
pattern = '\(.*\)'
fields = str(browser).split('\n')
textfields = []
for field in fields:
if 'Text' in field: textfields.append(field)
titleFieldName = re.findall(pattern, textfields[0])[0][1:-2]
locationFieldName = re.findall(pattern, textfields[1])[0][1:-2]
descriptionFieldName = re.findall(pattern, textfields[2])[0][1:-2]
I don't think mechanize has the exact functionality you require; could you use mechanize to get the HTML page, then parse the latter for example with BeautifulSoup?