django admin : TclError : Can't find a usable init.tcl - python

I use django, python 2.7, virtualenv
I try in the admin to open a file with a dialogbox
def import_csv(self, request, queryset):
import csv
from Tkinter import *
from tkFileDialog import *
fileName = askopenfilename()
I have the error :
Can't find a usable init.tcl in the following directories:
C:/Python27/lib/tcl8.5 C:/mat4/env/lib/tcl8.5 C:/mat4/lib/tcl8.5 C:/mat4/env/library C:/mat4/library C:/mat4/tcl8.5.2/library C:/tcl8.5.2/library
This probably means that Tcl wasn't installed properly.
I tried to use easygui but it is the same
How to fix this error ?

This can't possibly work. Django is a web framework. You can't run a desktop GUI like Tkinter in a website.
You need to create a web form with a file field and a view to process the upload.

Related

How to add jinja2 extensions to python bottle

I want to add the minify html extension found here to the Jinja2Template in bottle.
In bottle.py I've gotten as far as changing the line
self.env = Environment(loader=FunctionLoader(self.loader), **kwargs)
to
self.env = Environment(loader=FunctionLoader(self.loader), extensions=['jinja2htmlcompress.HTMLCompress'], **kwargs)
It doesn't know where to get the extension so I get this error:
ModuleNotFoundError("No module named 'jinja2htmlcompress'",)
How do I make the module available?
furas was right. I just put jinja2htmlcompress.py in the same folder as bottle.py and it worked.
You can also add a model directory to your project with an an empty __init__.py file and any extension files you want to use (in my case jinja2htmlcompress.py). Then add from model import jinja2hmtlcompress
to bottle.py

Django: manipulating database with script; importation error

I'm going through the Django tutorial and wanted to try out database manipulating with a python script but encounter a problem
My script:
from polls.models import Question, Choice
from django.utils import timezone
import numpy as np
#Questions
nquestions=3
q=np.empty([nquestions, 10], dtype=object)
qtext=q
qtext[0]="What's up?"
qtext[1]="What's new?"
qtext[2]="What's old?"
#Choices
q[0,1]="Not much"
q[0,2]="The sky"
q[1,1]="This question"
q[1,2]="This answer"
q[2,1]="No more originality"
q[2,2]="xxxxxxx"
#Check if exists and apply
for i in range(0, len(q)):
q[i,0]=Question(question_text=qtext[i], pub_date=timezone.now())
if Question.objects.filter(question_text=qtext[i]).exists():
pass
else:
q[i,0].question_text=qtext[i]
q[i,0].save()
alen=len(q[i][q[i] != np.array(None)])
for ii in range(0, alen-1):
q[i,0].choice_set.create(choice_text=q[i,ii+1], votes=0)
I get the error django.core.exceptions.appregistrynotready apps aren't loaded yet. I'm running the script from terminal and it's places in the folder that contains the polls-folder (One level over modules.py, which I'm trying to import).
Writing the contents of my script works if i first run in terminal : python manage.py shell. Is there a way for me to run a script to manipulate the database through Djangos API, or do I have to enter information manually or send requests thŕough post?
You need to set the DJANGO_SETTINGS_MODULE environment variable and call django.setup() before you can import your models.
import os
import django
# Change mysite if your project has a different name
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
from polls.models import Question, Choice
...
See the docs for more info.
Another option is to write a custom management command. When you use manage.py to run your management command you don't have to set DJANGO_SETTINGS_MODULE or call django.setup().

Difficulty opening sql database

what i m trying to do is point to a relative path inside my django app.
I do have to mention that test.py, forms.py and search.py are all in the same directory
I have 2 .py files, the first one is this:
Test.py
import os
import sqlite3
docs= os.path.abspath('..').rsplit("\\",1)[0]
datab=(docs+ "\\BMS\\Database\\Database.db")
datab.encode('unicode-escape')
conn=sqlite3.connect(datab)
This works fine, while i have the exact same code in search.py, inside a class which i import in forms.py which is part of django
search.py
import sqlite3
import os
class SearchBy(object):
docs= os.path.abspath('..').rsplit("\\",1)[0]
datab=(docs+ "\\BMS\\Database\\Database.db")
datab.encode('unicode-escape')
conn=sqlite3.connect(datab)
def get_countries(self):
do something
and finally forms.py
from django import forms
import sqlite3
from .search import SearchBy
class searchForm(forms.Form):
search_class=SearchBy()
lista_tari=get_countries()
country=forms.ChoiceField(choices=lista_tari, widget=forms.Select(), initial=0,required=True)
I simply cannot uderstand why i am given this error:
sqlite3.OperationalError: unable to open database file
Under the circumstances that in the other file everything works fine.
Check your file architecture , when you include a .py file in python, the path in included file , is imported as it is.
Meaning if they arent in the same folder , the path is different between the 2

How can I import TemplateDoesNotExist in app engine (python)?

I can't believe I'm not able to find this, but this is quite simple:
from google.appengine.ext.webapp import template
try :
content = template.render(...)
except TemplateDoesNotExist:
content = ...
Then I don't know how to import TemplateDoesNotExist! Can anyone tell me its module path? Thanks!
TemplateDoesNotExist is defined in django.template However from what you are doing it appears your going down a deprecated path under Python 2.7 . If you have a look at the template.py you just imported from google.appengine.ext.webapp import template you will see the following deprecation warning.
_PYTHON27_DEPRECATION = (
'google.appengine.ext.webapp.template is deprecated. Please use another '
'templating system such as django.template or jinja2.')
You haven't said if your using python 2.5 or python 2.7 . If your starting a new project the recommendation from google is to use 2.7.
If you're using python 2.7 and importing template as
from google.appengine.ext.webapp import template
Then you can import the exception as
from google.appengine._internal.django.template import TemplateDoesNotExist
and then
try:
template.render(...)
except TemplateDoesNotExist:
# do something useful

app engine python is this bug: when open switch interactive console

While I am working at localhost:8080, when I open interactive console and do some operations, like getting list of Kind etc (address: http://localhost:8080/_ah/admin/interactive) then it gives me this error:
<class 'google.appengine.dist._library.UnacceptableVersionError'>: django 1.2 was requested, but 0.96.4.None is already in use
This errors happened several times, in similar cases. It is stuck until restart localhost by dev_appserver.py
Is this a bug or what I am doing wrong?
Example for what I did at interactive console:
from myapp.models import *
for room in Room.all():
room.update_time = room.create_time
room.put()
Note:
This is my django_bootstrap :
import os
import sys
import logging
import __builtin__
from google.appengine.ext.webapp import util
import pickle
sys.modules['cPicle'] =pickle
logging.getLogger().setLevel(logging.INFO)
sys.path.insert(0, os.path.abspath((os.path.dirname(__file__))))
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')
import django.core.handlers.wsgi
def main():
application = django.core.handlers.wsgi.WSGIHandler()
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
my index.ymal in root folder says:
# AUTOGENERATED
# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run. If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED"). If you want to manage some indexes
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
Thus each time I open http://localhost:8080/_ah/admin/datastore, this file updated: which is still has the same content but timestamp of file on operating system says it is updated.
I think here, As the http://localhost:8080 sees that models.py is not the same then it could load it then can not start django_bootstrap.
However if I first open http://localhost:8080/_ah/admin/datastore and then http://localhost:8080, it works. So this is why sometimes I get error sometimes not: It depends of order urls respective

Categories

Resources