How to get the current URL within a Django template?

Django 1.9 and above: ## template {{ request.path }} # -without GET parameters {{ request.get_full_path }} # – with GET parameters Old: ## settings.py TEMPLATE_CONTEXT_PROCESSORS = ( ‘django.core.context_processors.request’, ) ## views.py from django.template import * def home(request): return render_to_response(‘home.html’, {}, context_instance=RequestContext(request)) ## template {{ request.path }}

(13: Permission denied) while connecting to upstream:[nginx]

Disclaimer Make sure there are no security implications for your use-case before running this. Answer I had a similar issue getting Fedora 20, Nginx, Node.js, and Ghost (blog) to work. It turns out my issue was due to SELinux. This should solve the problem: setsebool -P httpd_can_network_connect 1 Details I checked for errors in the … Read more

What’s the difference between CharField and TextField in Django?

It’s a difference between RDBMS’s varchar (or similar) — those are usually specified with a maximum length, and might be more efficient in terms of performance or storage — and text (or similar) types — those are usually limited only by hardcoded implementation limits (not a DB schema). PostgreSQL 9, specifically, states that “There is … Read more

Can “list_display” in a Django ModelAdmin display attributes of ForeignKey fields?

As another option, you can do look ups like: class UserAdmin(admin.ModelAdmin): list_display = (…, ‘get_author’) def get_author(self, obj): return obj.book.author get_author.short_description = ‘Author’ get_author.admin_order_field = ‘book__author’ Since Django 3.2 you can use display() decorator: class UserAdmin(admin.ModelAdmin): list_display = (…, ‘get_author’) @display(ordering=’book__author’, description=’Author’) def get_author(self, obj): return obj.book.author

NumPy array is not JSON serializable

I regularly “jsonify” np.arrays. Try using the “.tolist()” method on the arrays first, like this: import numpy as np import codecs, json a = np.arange(10).reshape(2,5) # a 2 by 5 array b = a.tolist() # nested lists with same data, indices file_path = “/path.json” ## your path variable json.dump(b, codecs.open(file_path, ‘w’, encoding=’utf-8′), separators=(‘,’, ‘:’), sort_keys=True, … Read more

Convert Django Model object to dict with all of the fields intact

There are many ways to convert an instance to a dictionary, with varying degrees of corner case handling and closeness to the desired result. 1. instance.__dict__ instance.__dict__ which returns {‘_foreign_key_cache’: <OtherModel: OtherModel object>, ‘_state’: <django.db.models.base.ModelState at 0x7ff0993f6908>, ‘auto_now_add’: datetime.datetime(2018, 12, 20, 21, 34, 29, 494827, tzinfo=<UTC>), ‘foreign_key_id’: 2, ‘id’: 1, ‘normal_value’: 1, ‘readonly_value’: 2} This … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)