python django API authentication post request - output variable displaying json string in browser, but NOT when printed
Date : March 29 2020, 07:55 AM
should help you out , you should instead use result.read()
|
django python: how to make json from django queryset
Tag : python , By : Lex Viatkine
Date : November 14 2020, 06:58 AM
like below fixes the issue Firstly, there's an error in your expected output. You probably meant: [{"pid": 1,
"s": [{"sid": 1, "sd": "south-1"},
{"sid": 2, "sd": "north-5"},
{"sid": 3, "sd": "west-3"}
],
"loc": "KL"
}]
>>> q = ... # as above
>>> r = {"pid": q[0]["pid"], "loc": q[0]["loc"]} # since pid and loc are always the same
>>> r["s"] = [{"sid": x["sid"], "sd": x["sd"]} for x in q]
>>> print r
[{'pid': 1,
's': [{'sid': 1, 'sd': 'south-1'},
{'sid': 2, 'sd': 'north-5'},
{'sid': 3, 'sd': 'west-3'}
],
'loc': 'KL'
}]
>>> print json.dumps(r) # gives the output as a json string
|
Pass python/django variable into javascript as JSON?
Date : March 29 2020, 07:55 AM
To fix this issue Woking on personal project website with django 1.9 and python 3.4. I am using FullCalendar. The idea is to pass a set of appointment objects into the html page containing the javascript for the calendar. But right now, I am just trying to pass a single default appointment. , Just use the safe filter: var data = jQuery.parseJSON('{{appt | safe}}');
var apptData = {{ appt | safe }};
|
Error while using function name as a variable in Pycharm, Python, Django: local variable 'processed' referenced before a
Tag : python , By : user157138
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I use Pycharm Professional 2019.2, Python 3.7.4, Django 2.2.5. As I know, function names are global variables in modules. But I have a function that denies that. , One simple solution is to do: def processed(request):
# Do not use the function name as the parameter name.
ret = processed
# It should be 'POST', not 'post'.
if request.method == 'POST':
# It should be 'POST', not 'post'.
text = request.POST['text']
ret = text.upper()
return HttpResponse(ret)
|
Python / Django / Jinja2: How to extend a variable name with the value of another variable (e.g. in a for-loop)?
Date : March 29 2020, 07:55 AM
it should still fix some issue The recommended approach for complex logic in templates is to write the logic in a model method, rather than in the template itself. In your case, I think this is what you want: class SomeModel(models.Model):
slider_image_1 = models.ImageField(upload_to='images/', blank=True)
slider_image_2 = models.ImageField(upload_to='images/', blank=True)
slider_image_3 = models.ImageField(upload_to='images/', blank=True)
slider_image_4 = models.ImageField(upload_to='images/', blank=True)
slider_image_5 = models.ImageField(upload_to='images/', blank=True)
@property
def slider_images(self):
fields = [field.name for field in self._meta.get_fields()]
results = []
for field in fields:
if 'slider_image' in field:
data = getattr(self, field)
if data:
results.append(data.url)
return results
@property
def slider_images(self): # simplified version
results = [getattr(self, field.name) for field in self._meta.get_fields()
if 'slider_image' in field]
return [result.url for result in results if result]
{% for url in object.slider_images %}
<img src="{{ url }}">
{% endfor %}
|