On Tue, Nov 20, 2012 at 2:07 PM, Nebros <markuschriste...@gmail.com> wrote:
> so pls just help me by this problem, cause i do not know everything about
> programming with django.

The problem is that there is a lot going wrong in your code than just
not knowing Django.

I would recommend some python training, or a good book on python in
your native language. I'd also repeat what Martin told you, you are
using Django without using the features in Django that make it
worthwhile using Django. This will make life very hard for yourself,
as you are seeing.

Anyhow, to your code:

> def kundendaten(request):
>     def get_queryset(self):
>         kunde = self.request.GET.get("kunde", False)
>         if kunde == "all":
>             kunde = False
>         if kunde:
>             variable = kunde
>         return get_queryset(variable)

What is this function - get_queryset() -  supposed to do? It has an
argument of 'self', but this is not inside a class, so using self is
misleading. You never call this function, which is a good thing as it
never returns anything and recurses into itself. Calling this function
in python would lead to python crashing with a RuntimeError for
reaching maximum recursion depth.

>     variable ="x"
>     cnxn = pyodbc.connect('DRIVER={SQL
> Server};SERVER=MAURITIUS;DATABASE=baan5c;UID=***;PWD=*****')
>     cursor = cnxn.cursor()
>     cursor.execute("SELECT x.t_name, y.t_mail FROM tttaad200000 as x,
> tttcmf200000 as y WHERE (x.t_name = y.t_name) AND (x.t_user = %r)" %x)

C'mon. This code doesn't even run does it? It should blow up at this
point with a NameError - you have a variable called 'variable' that
has a value of 'x', but you have no variable called 'x'.

Forgetting that. you've got a cursor there. Cursors all behave
according to the Python DB API, which has methods of specifying
arguments you want inserted into the query:

http://wiki.python.org/moin/DbApiFaq

The reason we use an API, is that they do some of the hard bits of
using a database for you. By manually constructing the SQL string, you
are allowing the opportunity for SQL injection. This is something the
DB API would handle for you. It's also handled if you were using
Django's ORM (which we use as it does ALL the hard bits for us).

Using the DB API seems quite challenging, even if experienced, which
is why we use Django's ORM. You would be better spent getting the ORM
to work with your setup than hacking in bits like this. If you are
going to continue to write your own SQL, it might also be worth
looking up what a JOIN is.

>     row = cursor.fetchall()

As I understand it 'row' is in fact multiple rows. It makes sense to
use plural names when appropriate.

>     now = datetime.datetime.now()
>
>     return render_to_response("kundendaten.html", { 'row':
> row,'current_date': now}, context_instance=RequestContext(request))


Now the template:

> {% include "header.html" %}
> <title>Kundendaten</title>
> {% include "header2.html" %}
> <h1>Portal</h1>
> <h2>Ausgabe Kundendaten</h2>
> {% block content1 %}<p>Zeit der Aktualisierung {{ current_date }}</p>{%
> endblock %}

{% block %} allows you to define parts of a template that get replaced
by a template that inherits from this one. Typically, rather than
include multiple header and footer templates, you would instead define
one base template, that has blocks for your title, your content, etc.

The template each view renders is then an derived version of this base
template, and the named blocks in the parent template are replaced by
the same named blocks in the child template.

If you aren't using template inheritance, then the block tag is
irrelevant and has no meaning.

> {% block content2 %}<p>Kundendaten {{ row }}.</p>{% endblock %}
> <table border="1">
>     <tr>
>         <th>Name</th>
>         <th>E-Mail</th>
>     </tr>
>     {% for t_name in row %}
>     <tr>
>         <td>{{ row.t_name }}</td>
>         <td>{{ row.t_mail }}</td>
>     </tr>
>     {% endfor %}

"for val in list" iterates through 'list' yielding a context variable
named 'val' in each iteration of the loop. Your list is called 'row',
which is a list of tuples, which you extracted from the database, and
you call the per-loop variable 't_name'. Yet inside the loop, you
never refer to 't_name' and instead try to lookup attributes from your
list of rows called 'row'. This is not going to work.

Remember when I said use plural names for plural objects? If your list
of rows was called 'rows', that line would read 'for row in rows',
which makes a lot more sense.


So, apart from all the bugs, is what you are trying to do is add a
parameter specified in the GET parameters in the request into that SQL
query?

kunde = request.GET.get("kunde", 'your default "kunde"')
sql = """
SELECT x.t_name, y.t_mail
FROM tttaad200000 as x, tttcmf200000 as y
WHERE (x.t_name = y.t_name)
AND (x.t_user = ?)
"""
cursor.execute(sql, kunde)

Even if your project means you cannot use Django's ORM, I would still
setup a simple full-stack Django project, do the tutorial and
experiment a bit, in order to give you a bit of experience actually
using Django and understanding how everything fits together.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to