Question about QuerySet and Pagination
Hello, I'm currently trying to paginate my results from a search query. views.py: def search(request): query = request.GET.get('q', '') if query: qset = ( Q(NAME__icontains=query) ) results = Thin_Client.objects.filter(qset).distinct() else: results = [] paginator = Paginator(results, 25) try: page = int(request.POST.get('page', '1')) except ValueError: page = 1 try: thin_client = paginator.page(page) except (EmptyPage, InvalidPage): thin_client = paginator.page(paginator.num_pages) return render_to_response("thin_clients/thin_client_search.html", { "results": results, "query": query, }) thin_client_search.html: {% block coltype %}colMS{% endblock %} {% block content %} Search Search: {% if query %} Results for "{{ query|escape }}": {% if results %} The Thin Client Name Thin Client IP The Thin Client MAC Address Inventar Nummer Einstellungen des Thin Clients {% for Thin_Client in results %} {{ Thin_Client.NAME }} {{ Thin_Client.IP }} {{ Thin_Client.MAC }} {{ Thin_Client.INVENTAR }} Details {% endfor %} {% else %} No Thin Clients found {% endif %} {% endif %} {% endblock %} {% block pagination %} {% if results.has_previous %} {% endif %} Page {{ results.number }} of {{ results.paginator.num_pages }} {% if results.has_next %} {% endif %} {% endblock %} urls.py: ... url(r'^search/$', 'thinco.views.search'), ... Now It just won't work and I have to admit I don't quite understand why. Any help is appreciated. -- 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.
Re: Question about QuerySet and Pagination
Ok, so now i have: template: {% block coltype %}colMS{% endblock %} {% block content %} Suche Search: {% if query %} Resultate für "{{ query|escape }}": {% if results %} The Thin Client Name Thin Client IP The Thin Client MAC Address Inventar Nummer Einstellungen des Thin Clients {% for Thin_Client in thin_client.object_list %} {{ Thin_Client.NAME }} {{ Thin_Client.IP }} {{ Thin_Client.MAC }} {{ Thin_Client.INVENTAR }} Details {% endfor %} {% else %} No Thin Clients found {% endif %} {% endif %} {% endblock %} {% block pagination %} {% if thin_client.has_previous %} {% endif %} Seite {{ thin_client.number }} von {{ thin_client.paginator.num_pages }} {% if thin_client.has_next %} {% endif %} {% endblock %} view: def search(request): query = request.GET.get('q', '') if query: qset = ( Q(NAME__icontains=query) ) results = Thin_Client.objects.filter(qset).distinct() else: results = [] paginator = Paginator(results, 25) try: page = int(request.POST.get('page', '1')) except ValueError: page = 1 try: thin_client = paginator.page(page) except (EmptyPage, InvalidPage): thin_client = paginator.page(paginator.num_pages) return render_to_response("thin_clients/thin_client_search.html", { "results": results, "query": query, "thin_client": thin_client }) urls: url(r'^$', 'thinco.views.list'), url(r'^thin_client/$', 'thinco.views.list'), url(r'^thin_client/page(?P[0-9]+)/$', 'thinco.views.list'), url(r'^thin_client/(?P[a-zA-Z0-9_.-]+)/$', list_detail.object_detail, thin_client_detail), url(r'^edit/thin_client/(?P[a-zA-Z0-9_.-]+)/$', create_update.update_object, thin_client_update), url(r'^search/$', 'thinco.views.search'), ... Now if i click next page I get an empty page because: http://127.0.0.1:8000/search/?page=2, I probably need a url entry and do i have to give the poaginator the query or do I have to save the query somewhere? Also thank you for your help. On Sep 29, 3:51 pm, Tom Evans wrote: > On Thu, Sep 29, 2011 at 8:36 AM, Felix Wagner > wrote: > > Hello, > > > I'm currently trying to paginate my results from a search query. > > > views.py: > > > def search(request): > > query = request.GET.get('q', '') > > > if query: > > qset = ( > > Q(NAME__icontains=query) > > ) > > results = Thin_Client.objects.filter(qset).distinct() > > > else: > > results = [] > > > paginator = Paginator(results, 25) > > try: > > page = int(request.POST.get('page', '1')) > > except ValueError: > > page = 1 > > try: > > thin_client = paginator.page(page) > > except (EmptyPage, InvalidPage): > > thin_client = paginator.page(paginator.num_pages) > > > return render_to_response("thin_clients/thin_client_search.html", > > { > > "results": results, > > "query": query, > > }) > > You need to actually pass the paginated page ('thin_client') to your > template and iterate over that. You are still iterating over the > unpaginated 'results', so unsurprisingly you don't see the pagination. > See the view example in the docs: > > https://docs.djangoproject.com/en/1.3/topics/pagination/#using-pagina... > > 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.
Re: Question about QuerySet and Pagination
Thank you very much for your help On Sep 29, 4:25 pm, Tom Evans wrote: > On Thu, Sep 29, 2011 at 3:16 PM, Felix Wagner > wrote: > > Ok, so now i have: > > .. > > > ... > > Now if i click next page I get an empty page because: > >http://127.0.0.1:8000/search/?page=2, > > > I probably need a url entry and do i have to give the poaginator the > > query or do I have to save the query somewhere? > > Also thank you for your help. > > If your view is based upon a query/parameters, its up to you to > persist that in the pagination URLs, so you will need to extract the > search parameters (request.META['QUERY_STRING']) and pass them through > to the template so you can include them in the pagination urls. > > Be sure that you don't also include the 'page' parameter multiple > times on subsequent pages - eg ?q=cheese&page=2&page=3 is not ideal. > > 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.
Django + Ajax/Javascript
Hi, I have the following model layout: class A(models.model): options = models.ManyToManyField(OptionSet, blank=True, null=True) values = models.ManyToManyField(Value, blank=True, null=True) class OptionSet(models.model): name = models.TextField(unique=True) values = models.ManyToManyField(Value) def __unicode__(self): return '%s' % self.name class Value(models.Model): name = models.TextField() key = models.ForeignKey(Key, related_name='values') class Key(models.Model): name = models.TextField(unique=True) And my forms.py looks like this: class A_Form(ModelForm): values = forms.ModelMultipleChoiceField(queryset=Value.objects.all(), widget=CheckboxSelectMultiple, label="Einzelne Werte", required=False) options = forms.ModelMultipleChoiceField(queryset=OptionSet.objects.all(), widget=CheckboxSelectMultiple, label="Optionen Sets", required=False) Template: {% csrf_token %} {{ form.as_table }} I use that form with a generic update view! I'm new to javascript/ajax to be honest never did something in javascript/ajax. What I want to do is on mouseover on the options name it should show all the values for that option set. How would one accomplish this (either with or without ajax)? -- 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.
Python inside Django
Hello, currently I am writing an application that will run a daemon. However I am stuck: What I have so far: created a project called "ihe", with an app called "devices". Inside devices there is "admin.py", "forms.py", "__init__.py", "models.py", "urls.py", "views.py". There are also several directories: "configuration" contains "config.py", "__init__.py" and "logging.conf", the "daemons" directory contains the daemons that I want to execute. A standard "managment" directory containing the management calls. And lastly a "utilities" direcotry containing "hwcontrol.py" and "utils.py". My issue is that if I want to for instance import hwutils which is pure python inside a daemon or a "from devices.models import *" inside a daemon or any other file it always complains about an import error: "Traceback (most recent call last): File "database.py", line 14, in from devices.models import * ImportError: No module named devices.models" "Traceback (most recent call last): File "database.py", line 14, in from devices.utilities import hwcontrol "ImportError: No module named devices.utilities" If I understand correctly this is because of the python path. For instance the pythonpath for a daemon: ['/~/software/ihe/devices/daemons', '/usr/lib/portage/pym', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/Orange/orng', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib64/python2.7/site-packages/wx-2.8-gtk2-unicode', '/usr/lib64/portage/pym'] However I thought that if I put the files inside the django project that the imports would then work. What do I have to do to get it working? Thank you for any help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Django management call_command
Hello, I wrote a test management Command: def start_daemon(): pidfile_rule = "/tmp/ihe_test.pid" p = str(os.getpid()) file(pidfile_rule, 'w').write(p) while True: print "Hello" sleep(10) class Command(BaseCommand): help = 'Starts or stops the daemons' option_list = BaseCommand.option_list + ( make_option('--start', action='store_true', dest='status', help='Start the daemons'), make_option('--stop', action='store_false', dest='status', default=False, help='Stop the daemons'), ) def handle(self, *args, **options): status = options.get('status') pidfile_rule = "/tmp/ihe_test.pid" if status: try: start_daemon() except Exception, e: print e else: x = file(pidfile_rule, 'r').read() os.kill(int(x), signal.SIGKILL) Now what I want to accomplish: In a form if a boolean field is True then execute (start) the daemon. If the boolean field is toggled False the daemon should be stopped. However the problem is: wagnerf+ 5882 0.3 0.1 108892 21388 pts/3S+ 17:32 0:00 /usr/bin/python2.7 manage.py runserver wagnerf+ 5883 51.5 0.4 580376 52292 pts/3Rl+ 17:32 0:35 /usr/bin/python2.7 manage.py runserve One is the normal runserver and one is the daemon. Obviously I can't use the site anymore. How exactly must it be coded so that the managment call is a seperate process? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.