Trying to find something in multiple databases... Confused...

75 views
Skip to first unread message

Laurence MacNeill

unread,
Jun 18, 2012, 3:40:53 AM6/18/12
to django...@googlegroups.com
Ok, I'm a total django noob here, so I am probably doing this wrong...  But here goes...

When someone comes to my django app's root (index), I need to verify their user-name (which is stored in a Linux environment variable).  Based on where (or if) I find their user-name, I want to send them to one of three different pages -- if I don't find them in any database, I want to add them to a given database, then send them to a page.

So here's what I have in my views.py file:
def index(request)
     current_username = os.environ['REMOTE_USER']

Laurence MacNeill

unread,
Jun 18, 2012, 3:52:35 AM6/18/12
to django...@googlegroups.com
well -- I hit the wrong key and posted that before I was finished typing...

here's what's in my views.py file:

def index(request)
     current_username = os.environ['REMOTE_USER']

This should provide me with their username (yes I do have 'import os' at the top of the file)...

Now, after I get their username I want to do something like this (but I'm not sure how to write in django -- so I've put ## around the pseudo-code)
     try:
         student = Student.objects.get(student_name=current_username)
     except (#can't find it in Student database, but I don't know what code to put here#)
         try:
             instructor = Instructor.objects.get(instructor_name=current_username)
         except (#can't find it in Instructor database, but I don't know what code to put here#)
               try:
                    administrator = Administrator.objects.get(administrator_name=current_username)
               except (#fail condition - can't find current_username in Administrator database, but I don't know what code to put here#)
                    #because we failed to find it in any database, we assume it's a student and add it
                    student = Student(student_name=current_username)
                    student.save
     #here's where I'm not sure how to continue -- I want to do something like this#
     #if student is defined#
          return render_to_response('ta/student.html', student)
     #else if instructor is defined#
          return render_to_response('ta/instructor.html', instructor)
     #else if administrator is defined#
          return render_to_response('ta/administrator.html', administrator)
#end of my code#

So -- can someone please help me fill in the missing code above?  I've been searching for hours, but I just can't seem to hit on the correct search-terms to get done what I want here...

Thanks,
Laurence MacNeill
Mableton, Georgia, USA

Melvyn Sopacua

unread,
Jun 18, 2012, 7:05:53 AM6/18/12
to django...@googlegroups.com
On 18-6-2012 9:52, Laurence MacNeill wrote:
> well -- I hit the wrong key and posted that before I was finished typing...
>
> here's what's in my views.py file:
> def index(request)
> current_username = os.environ['REMOTE_USER']

And you're sure this works? Try:
return django.shortcuts.render_to_response(current_username)

here to verify it's coming through. Normally, the authenticated username
would be part of the request dictionary.

> This should provide me with their username (yes I do have 'import os' at
> the top of the file)...
>
> Now, after I get their username I want to do something like this (but I'm
> not sure how to write in django -- so I've put ## around the pseudo-code)
> try:
> student = Student.objects.get(student_name=current_username)
> except (#can't find it in Student database, but I don't know what code
> to put here#)

It's in tutorial part 3:
https://docs.djangoproject.com/en/1.4/intro/tutorial03/#raising-404

except Student.DoesNotExist:
student = None

> #here's where I'm not sure how to continue -- I want to do something
> like this#
> #if student is defined#
> return render_to_response('ta/student.html', student)

Almost correct:
if student is not None :
# the student.html template will now have a variable named student
# which is the student object you fetched
return render_to_response('ta/student.html', student=student)
elif instructor is not None :
#etc

--
Melvyn Sopacua

Daniel Roseman

unread,
Jun 18, 2012, 8:12:17 AM6/18/12
to django...@googlegroups.com
So Melvyn has answered your question, but a quick note on terminology: what you've got there are separate *database tables*, or separate *Django models* -- not separate databases. Multiple databases is a whole different question, which you really don't want to get into as a newbie (or, indeed, at all if possible).
--
DR. 

kenneth gonsalves

unread,
Jun 18, 2012, 8:16:55 AM6/18/12
to django...@googlegroups.com
On Mon, 2012-06-18 at 05:12 -0700, Daniel Roseman wrote:
> Multiple databases is a whole different
> question, which you really don't want to get into as a newbie (or,
> indeed,
> at all if possible).

could you elaborate on this - I was thinking on getting into multiple
databases and would appreciate your take.
--
regards
Kenneth Gonsalves

Daniel Roseman

unread,
Jun 18, 2012, 9:30:26 AM6/18/12
to django...@googlegroups.com
Oh, I'm just being my usual crotchety self. There are certain "advanced" features of Django - multiple DBs, model subclassing, that sort of thing - that I tend to feel we'd be better off without. But I'm almost certainly being unfair, there is definitely a use case for multiple DBs: it's when people start trying to use it for things like dynamic multitenancy that I start to despair.
--
DR.

Kurtis Mullins

unread,
Jun 18, 2012, 10:32:05 AM6/18/12
to django...@googlegroups.com
On Mon, Jun 18, 2012 at 9:30 AM, Daniel Roseman <dan...@roseman.org.uk> wrote: 
There are certain "advanced" features of Django - multiple DBs, model subclassing, that sort of thing

I feel there's quite a few problems that would be relatively unsolvable without model subclassing. At least in any efficient way.
Message has been deleted

Kurtis Mullins

unread,
Jun 18, 2012, 2:16:51 PM6/18/12
to django...@googlegroups.com
On Mon, Jun 18, 2012 at 1:45 PM, Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:

       First, is everybody on the same page (terminology)... (Independent
of Django)

       First is: multiple database engines (SQLite3, MySQL, Access/JET,
etc.). Working across multiple engines is never easy -- one typically
has to implement the equivalent of "select ... from ... join ..." in
program code -- that is, fetch records from each engine and merge them
by hand to produce a result set.

       Second is: multiple databases within an engine. Database engines are
designed to allow for separate databases with independent user access --
a personnel/payroll database would not be accessible by the parts
order/tracking system, even when both are stored on the same database
engine/server -- however, an application /might/ be permitted such an
odd mix if the user authorization permits access to both databases. For
SQLite3 and Access/JET, a database is a single file (no particular
extension for SQLite3, Access used to use .MDB) and an application
typically opens/connects to just one of these -- but means are available
to "link" a second into visibility.

       Third: a database, as defined in #2, contains multiple relations
(tables) which may or may not be interconnected by foreign key
references (in relational database theory, a relation is a single table
in which the data of each tuple (record) is related to a unique key for
the tuple; relation does NOT refer to linkages by foreign keys between
tables).

I believe, by reading the context, it was pretty obvious he meant databases (definition #2)  being managed under multiple database management systems, without regards to their specified database engine. 

By the way, MySQL is not a database engine -- it is a database management system. MyISAM and InnoDB would be database engines.

Message has been deleted

Blaxton

unread,
Jun 19, 2012, 2:37:02 AM6/19/12
to django...@googlegroups.com
I am a newbie in Django and in need of some hints to point in the right direction.

I am building a new application which users would enter some names and then
after clicking on submit, some non .html files should be created or modified
while entered data should be inserted in some parts of the file that have been created
or modified. also those files are not going to be served in web, they are just
some static files located at different locations in system for other uses.


should I start using template  to generate those non html files ?
does Django has a module to create a file and capable of insert parameters to file ?
which python template is best suited and supported by Django ? jinja2 or mako or cheetah ?

what is the best way of calling C++ programs within Django ?



Laurence MacNeill

unread,
Jun 19, 2012, 3:46:32 AM6/19/12
to django...@googlegroups.com


On Monday, June 18, 2012, Melvyn Sopacua wrote:
On 18-6-2012 9:52, Laurence MacNeill wrote:
> well -- I hit the wrong key and posted that before I was finished typing...
>
> here's what's in my views.py file:
> def index(request)
>      current_username = os.environ['REMOTE_USER']

And you're sure this works? Try:
 return django.shortcuts.render_to_response(current_username)

here to verify it's coming through. Normally, the authenticated username
would be part of the request dictionary.


Yeah, it's not working...  How do I get the user-name from the request dictionary?  request.REMOTE_USER or somthing like that?

It's not using Django for the user-validation, though...  When someone logs into the site, they get redirected to a page that validates them.  This is controled by the Apache web-server itself -- if they try to access any document served by Apache, and they don't have a cookie on their computer, they're redirected to a different page where they enter their user ID and password, then are sent back to the original page.  The user-id is then stored in a linux environment variable called REMOTE_USER.  So I figured the only way to access it was via the os.environ method.

 

Almost correct:
if student is not None :
    # the student.html template will now have a variable named student
    # which is the student object you fetched
    return render_to_response('ta/student.html', student=student)
elif instructor is not None :
    #etc

Ahh, ok -- that makes sense...  Thanks...

L.

Laurence MacNeill

unread,
Jun 19, 2012, 3:47:11 AM6/19/12
to django...@googlegroups.com
Right -- different tables in the same database, of course...  Thanks.

L.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/jSuHg6H4amoJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Blaxton

unread,
Jun 19, 2012, 3:40:40 AM6/19/12
to django...@googlegroups.com

kenneth gonsalves

unread,
Jun 19, 2012, 7:12:34 AM6/19/12
to django...@googlegroups.com
On Tue, 2012-06-19 at 00:40 -0700, Blaxton wrote:
> I am a newbie in Django and in need of some hints to
> point in the right direction.
>
> I am building a new
> application which users would enter some names and then
> after
> clicking on submit, some non .html files should be created or
> modified
> while entered data should be inserted in some parts of the file that
> have been created
> or modified. also those files are not going to be
> served in web, they are just
> some static files located at
> different locations in system for other uses.

why use django? just plain html using cheetah or mako or jinja and some
python will do the trick.
>
> should I start using template to generate those non html files ?
> does Django has
> a module to create a file and capable of insert parameters to file ?
> which python template is best suited and supported by Django ? jinja2
> or mako or cheetah ?

the django template language is best suited and supported by django - of
course you are free to use anything you want.
>
> what is the best way of calling C++
> programs within Django ?

subprocess.call()?
--
regards
Kenneth Gonsalves

Melvyn Sopacua

unread,
Jun 19, 2012, 7:47:53 AM6/19/12
to django...@googlegroups.com
On 19-6-2012 9:46, Laurence MacNeill wrote:
> On Monday, June 18, 2012, Melvyn Sopacua wrote:
>
>> On 18-6-2012 9:52, Laurence MacNeill wrote:
>>> well -- I hit the wrong key and posted that before I was finished
>> typing...
>>>
>>> here's what's in my views.py file:
>>> def index(request)
>>> current_username = os.environ['REMOTE_USER']
>>
>> And you're sure this works? Try:
>> return django.shortcuts.render_to_response(current_username)
>>
>> here to verify it's coming through. Normally, the authenticated username
>> would be part of the request dictionary.
>
>
>
> Yeah, it's not working... How do I get the user-name from the request
> dictionary? request.REMOTE_USER or somthing like that?
>
> It's not using Django for the user-validation, though... When someone logs
> into the site, they get redirected to a page that validates them. This is
> controled by the Apache web-server itself -- if they try to access any
> document served by Apache, and they don't have a cookie on their computer,
> they're redirected to a different page where they enter their user ID and
> password, then are sent back to the original page. The user-id is then
> stored in a linux environment variable called REMOTE_USER. So I figured
> the only way to access it was via the os.environ method.

I get the feeling you're mixing and matching server-side programming
languages. If you're using any variation of basic HTTP authentication,
then the operating system environment isn't a factor, but the CGI
environment is, which gets set "somewhere" based on module
implementation. Maybe you think it's an environment variable, because
Php stores it in $_SERVER. Or because you use plain CGI scripts and for
those CGI environment equals OS environment. If that's the case, then
what you're looking for is this:
https://docs.djangoproject.com/en/1.4/howto/auth-remote-user/

HTTP protocol details as background:
What really happens with HTTP authentication that way, is that the
server sends a 401 Authorization Required response to the browser. The
user then enters username and password and if the server finds this
combination to be correct, it returns a redirect or shows the requested
page. If not, the server sends a 403 Forbidden.
Per CGI 1.1 specification [1], the server shall set the variable
REMOTE_USER in the CGI environment if it successfully authenticated a user.

[1] http://tools.ietf.org/html/rfc3875#section-4.1.11
--
Melvyn Sopacua

Blaxton

unread,
Jun 19, 2012, 8:37:54 AM6/19/12
to django...@googlegroups.com
My understanding from template system was:
Django template system is to create html files on fly while returning
the resulting html to user's browser !!!! and the resulting static files
would not be saved any where in file system.

How can I save the created files by template system on file system ?






From: kenneth gonsalves <law...@thenilgiris.com>
To: django...@googlegroups.com
Sent: Tuesday, June 19, 2012 3:42:34 PM
Subject: Re: Writing or editing files using Django
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsub...@googlegroups.com.

Melvyn Sopacua

unread,
Jun 19, 2012, 9:10:01 AM6/19/12
to django...@googlegroups.com
On 19-6-2012 14:37, Blaxton wrote:
> My understanding from template system was:
> Django template system is to create html files on fly while returning
> the resulting html to user's browser!

Nope, that's just the default use. The template system can be used for
anything to return "text" based on a "context".

> How can I save the created files by template system on file system ?

You'd do that in your view, similar to how mail is sent in this example:
<https://docs.djangoproject.com/en/1.4/topics/forms/#processing-the-data-from-a-form>

Except that you use:
<https://docs.djangoproject.com/en/1.4/ref/templates/api/#rendering-a-context>

So your process is:
- show editing form to user using a HTML template that gets sent to browser
- validate the form and clean the data
- render the template that generates your file based on the form data
- write the file using standard python file methods
- redirect the user

It's similar to this:
https://docs.djangoproject.com/en/1.4/howto/outputting-csv/

except don't call response.write() but write to a file.
--
Melvyn Sopacua

Kurtis Mullins

unread,
Jun 19, 2012, 10:49:41 AM6/19/12
to django...@googlegroups.com
I agree. It's very easy to use the templates to do just that. Once you have them processed and saved as a variable (which would be a string) then you can do anything with them that Python is capable of. For example, you could use them to generate emails, save text files, etc...

As far as calling C++ goes -- you could use subprocess if the C++ is an actual executable program and not just a library you're trying to call or some source you've written yourself. In the latter two cases, ignore Django and think about this in terms of "Accessing C++ from Python". Googling that will lead to several answers. The easiest way I've found is to externalize C functions that call C++ code and use Python CTypes (included in the standard library) to call those external C functions.

Good luck!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages