Django NoReverseMatch Error

Django’s reverse method can be found under django.core.urlresolvers (new versions: django.urls). To be able to use it you should import it like so:

reverse method is used to generate urls so that you can use them in your template files, model methods, view methods etc. Why do we use them? We use them not to hard code our urls to adhere to DRY (Don’t Repeat Yourself) principle. Why am I talking about reverse? Because reverse method is where I encountered this NoReverseMatch error. Error message was something like this: Reverse for ‘xx’ with arguments ‘()’ and keyword arguments ‘{‘xx’: ‘xx’}’ not found. 1 pattern(s) tried: [‘(?P<xx>[-\\w]+)/(?P<xx>[-\\w]+)/$’] I replaced the variable names with “xx” above. Let’s see the bits of code[…]

django related_name

related_name is an attribute you use while defining relations like many-to-one, many-to-many, one-to-one etc. The value of this attribute is used as the name of the reverse relation between models. Example:

Here we define a many-to-one relation using a Foreign Key. A Site can have many Pages. The relation from a Site to its pages can be expressed as such:

A reverse relation can be defined from the Site to Pages as you see above. It is important to understand the importance of related_name attribute. related_name can save you from many headaches by preventing clashes from ever happening in your applications. Different models having a Foreign Key relation to the same “X” model could have clashes for using[…]

No ‘jar’ binary detected in PATH. I encountered this error message when I was trying to install RedHat JBoss BMP Suite for the first time. At first, I thought I needed to install JRE and include the bin folder in the path. Apparently, that was not the solution. What you need to do is install the JDK and include the bin folder of JDK in your “Path” system variable. It would look like this:

Right Click “Computer” -> “Properties” -> “Advanced System Settings” -> “Environment Variables”. There you will find “Path” under System Variables. Append the above line into this variable. Now JBOSS BMP Suite Installation jar should work. Hope this helps. Good Luck, Serdar

In my new Django project, I was trying to append a list of tuples (which contained more than 1 tuple in it) into another list of tuples. Then I got this error: ValueError: too many values to unpack (expected 2) I was using .append() method to perform this operation and apparently that is exactly where the problem was. Result: You cannot append a list of tuples with more than one item into another list of tuples using .append(). You need to use “+=” operator to do this. Hope this helps. Good Luck, Serdar

django-no-such-table

Django “no such table” error of death.. I recently encountered this “no such table” error while trying to run makemigrations command on PyCharm. I dropped database tables related to my app, cleaned django_migrations table for this app, cleared migrations folder as described in this post: http://big.info/2016/07/django-clear-migrations-re-migrate.html. Then I ran makemigrations… No such table.. I did lots of googling but none of the solutions I found was applicable to my case. After some inspection of the error messages generated, I found out that one of the forms in my forms.py was trying to access a database that I had deleted. I commented out this form’s code and re-run makemigrations command. This time it was successful. Hope this helps. Good Luck, Serdar

difference-library-framework

Recently, I heard 2 of my friends talking about the difference between a framework and a library, and I thought some may need a clear explanation to see the difference. Check out the simple diagram below. Library A library is essentially a set of functions that you can call, these days usually organised into classes. Each call does some work and returns control to the client. The defining characteristic of a library is that you are in control, you call the library. Framework A framework embodies some abstract design, with more behaviours built in. In order to use it you need to insert your behaviour into various places in the framework either by subclassing or by plugging in your own classes. The[…]

pycharm django breakpoint

Debugging a Django project in PyCharm is actually quite easy. All you need to do is add couple of breakpoints and hit that little debug button you see at the top right corner of the IDE. Now when you run your app, PyCharm’s Django debugger should stop at those breakpoints, right? Nope, not that straightforward. Breakpoints that won’t stop the debugger Check out the lines below for example. I first put breakpoints on these 2 lines but the debugger would not hit any of them: This one stops the debugger Then I added 1 more breakpoint and voila. Debugger stops now. Suspend You see. Not all the breakpoints will make the debugger stop there. But why? Why did it not stop at my[…]

clone virtual environment django

I wanted to write 1 more post in 2016 before the year ends. So, here it is 🙂 Let’s talk a little more about Virtual Environments. If you have a virtual environment you can back it up and re-create it later with the exact same installations in it. No need to worry about what and which versions were installed. Actually, what we will do is not cloning the Virtual Environment, it is just exporting the configuration of it (as I said this includes the installation and version info) which can be used later in a brand new Virtual Environment to make it an exact same copy of this exported one. FREEZING & INSTALLING This export of installation configurations is called “freezing”.[…]

javascript error undefined

I am finally back with another post after a while. I had this time period where I got busy with other things in life and felt a little tired for coding and blogging. That seems to be over now. Thank you my little angel. 🙂 Today I was working on my Django project and had this annoying error in my javascript code. My code kept saying Cannot read property ‘toLowerCase’ of undefined but why? What was happening? Apparently the problem was with the context of the function I was calling and the meaning if “this” parameter inside that function. I was trying to invoke a function attached to the “onchange” event of a select control. I was attaching the function to the[…]

ajax-in-django

Ajax request are everywhere in modern web applications. No matter which platform you are using for development, the main scheme is always the same. You send a request to a server side method using JavaScript, jQuery etc. and server processes the request, prepares the response and sends this response to client (browser). Finally, response is received and processed by the client-side code (script) and dynamically generated content is displayed to the users. Flow is the same in Django. So, what we need to implement here? 1- We need a view function to receive and handle the request 2- We need a mapping in urls.py file to map the request url (which will be specified in the requesting client-side jQuery script) to[…]