django-int object has no attribute keys

django – ‘int’ object has no attribute ‘keys’

Being new to Django, sometimes I face interesting problems. If it is very late and you are very tired, It may not always be easy to see the problem looking at the displayed error message like this int’ object has no attribute ‘keys’ error message I got in cursor.execute() method 🙂

This error was thrown on the line below:

cursor.execute(“select col1,col2 from table_name where col3 = %s”, integer_value)

The reason I got this error was that I had provided the integer directly as an argument but according to the documentation the second argument must be a sequence or mapping. Below solved the issue:

cursor.execute(“select col1,col2 from table_name where col3 = %s”, [integer_value])

Hope this helps.
Good Luck,
Serdar

Django NoReverseMatch Error

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 that led to this error:

Above is a method that gets the url for a model that is created in the system. To display the model, I browse to this absolute url address. myapp is the namespace as you will see down below.

As you can see, I am using name spaced  urls. Anyways..

Above is the url patter from my application’s urls.py.

Above is the url from my main (project) urls file.

As you can see, ultimately the url I am trying to reach is formed by joining these 2 url definition. What you must pay attention for is that there are 2 variables in this structure that needs to be passed to reverse method. And as you can see there is only one variable being passed to the reverse method above, and that is where the problem is.

All I had to do was to see this mistake and provide both variables as kwargs to the reverse method and the problem was gone. Like so:

 

Hope this helps.
Good Luck,
Serdar

django related_name

What is “related_name” used for in Django

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 the same default related_name values.

Explicitly specifying related_name values for your relations will save you a lot of troubles later.

Hope this helps.
Good Luck,
Serdar

RedHat JBOSS BPM Suite Installation Error – No ‘jar’ binary detected in PATH

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.

no-jar-binary-detected-in-path

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.

computer-properties

computer-environment-variables

Append the above line into this variable. Now JBOSS BMP Suite Installation jar should work.

Hope this helps.
Good Luck,
Serdar

Python – Difference between “.append()” and “+= []”

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 Error – django.db.utils.OperationalError: 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

Difference between a Framework and a Library

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.

difference-library-framework

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 framework’s code then calls your code at these points. The defining characteristic of a framework is Inversion of Control. The framework calls you, not the other way round. (This is known as the Hollywood Principle: “Don’t call us, we’ll call you.”) The framework is in control. The flow of control and the flow of data is managed by the framework.

Hope this helps.
Good Luck,
Serdar

pycharm django breakpoint

PyCharm – Django Debugger Won’t Stop At BreakPoints

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?

django-debug-button

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:

invalid-breakpoints-pycharm-django

This one stops the debugger

Then I added 1 more breakpoint and voila. Debugger stops now.

valid-breakpoint-pycharm-django

Suspend

You see. Not all the breakpoints will make the debugger stop there. But why? Why did it not stop at my breakpoints? The answer was simple.

Click Run -> View Breakpoints…

run-viewbreakpoints-pycharm

Now I see the Breakpoints window. Clicking through my breakpoints I realise that for the ones that debugger won’t stop at, the Suspend checkbox is not selected.

django-breakpoints-window

Coming from other IDEs you might assume (like I did) that putting a breakpoint would by default mean that the debugger would stop there but for PyCharm 2016.21994 this is not the case. Your breakpoints not only need to be enabled but also need to be marked as Suspend.

Hope this helps.
Good Luck,
Serdar

clone virtual environment django

Django – How to Clone a Virtual Environment

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.

clone virtual environment django

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”. Below is how you can do it in console.

First activate your virtual environment. Then type below:

Above line will create a text file whose content will be something like this:

If in the future you want to re-create this Virtual Environment all you need to do is open a console, activate your newly created Virtual Environment and type the below line:

 

Hope this helps.
Good Luck,
Serdar

javascript error undefined

Cannot read property ‘toLowerCase’ of 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 event “inline” as below:

Nothing wrong with that. The problem was that the function “myFunction” was defined in the scope of the page and I was using “this” in that function as if it referred to the select control itself while it was actually referring to the page.

Wrong:

Correct:

Instead of using “this”, I passed a parameter to myFunction and built the id of my select control to use it instead of “this”. The problem was gone.

If you are seeing the same error message you might have a similar problem in your code.

Hope this helps.
Good Luck,
Serdar