0

I have a model with 3 fields namely name, qualification and keyskills. The keyskills field is given input as comma separated values, e.g "c,c++,java", the name from a text box and qualification from a dropdown menu.

I want to display the keyskills to be stored in a separate row in the database. I have done it. However, when I give input with one empty value, for example, "c,c++,,java", the database should not accept the empty value in between – it should move on to the next value "java" instead.

My models.py

class form1(models.Model):
    name=models.CharField(max_length=20)
    qualification=models.CharField(max_length=20)
    keyskills=models.CharField(max_length=50)
    def __unicode__(self):
        return self.name,self.qualification,self.key

views.py

def formdisplay(request):
    if request.method=='POST':
        i1=request.POST.get("input1")
        i2=request.POST.get("drop1")
        i3=request.POST.get("input2")
        var=i3.split(",")
        for i in var:
            form1(name=i1,qualification=i2,keyskills=i).save()
        return HttpResponseRedirect('/formdisplay')
    else:
        return render(request,"formdisplay.html"

formdisplay.html

<html>
<head>
</head>
<body>
<form action="." method="post">{% csrf_token %}
<input type="text" name="input1">
<select name="drop1">
<option>B.E cse</option>
<option>B.E ece</option>
<option>B.E eee</option>
</select>
<input type="text" name="input2">
<input type="submit" value="save">
</form>
</body>
</html>
miikkas
  • 818
  • 1
  • 8
  • 25
Madanika
  • 119
  • 1
  • 8

2 Answers2

0

If I understood correctly, then you just need to add an if-condition to check whether the string is empty or not:

Demo:

>>> s = "c,c++,,java"
for i in s.split(','):
    if i:
        print i

c
c++
java

So, for your code it would be:

for i in var:
    if i:
       form1(name=i1,qualification=i2,keyskills=i).save()
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0
if i3 != None:
  #do something etc
elif i2 != None:
  #do something etc

I think you are trying to check null values, then do something like above - just make a check that it is none or not

Saad Abdullah
  • 2,252
  • 3
  • 29
  • 42
  • Comparisons to `None` [should be done](http://www.python.org/dev/peps/pep-0008/#programming-recommendations) with `is` or `is not` as explained [here](http://stackoverflow.com/a/3257957/2096560). Furthermore, the question was about skipping an empty string from the split version of `i3`. – miikkas Nov 19 '13 at 17:38