django-views

How do I use Logging in the Django Debug Toolbar?

给你一囗甜甜゛ 提交于 2019-12-02 20:04:47
I would like to output debug messages in my django app at different points in a view function. The docs for the django-debug-toolbar say it uses the build in python logging but I can't find any more information then that. I don't really want to log to a file but to the info pane on the toolbar. How does this work? You just use the logging module methods and DjDT will intercept and display them in the Logging Panel. import logging logging.debug('Debug Message') if some_error: logging.error('Error Message') Logging straight to the root logger, as @jonwd7 mentioned, is usually not recommended.

Django - Getting last object created, simultaneous filters

末鹿安然 提交于 2019-12-02 19:57:09
Apologies, I am completely new to Django and Python. I have 2 questions. First, how would I go about getting the last object created (or highest pk) in a list of objects? For example, I know that I could use the following to get the first object: list = List.objects.all()[0] Is there a way to get the length of List.objects? I've tried List.objects.length but to no avail. Second, is it possible to create simultaneous filters or combine lists? Here is an example: def findNumber(request, number) phone_list = Numbers.objects.filter(cell=number) I want something like the above, but more like: def

Django search fields in multiple models

霸气de小男生 提交于 2019-12-02 19:51:10
I want to search multiple fields in many models. I don't want to use other apps like 'Haystack' only pure Django. For example: # models.py class Person(models.Model): first_name = models.CharField("First name", max_length=255) last_name = models.CharField("Last name", max_length=255) # other fields class Restaurant(models.Model): restaurant_name = models.CharField("Restaurant name", max_length=255) # other fields class Pizza(models.Model): pizza_name = models.CharField("Pizza name", max_length=255) # other fields When I type a 'Tonny' I should get a: "Tonny Montana" from Person model "Tonny's

Django:How can I prevent authenticated user from both register and login page in Django-registration-redux

我们两清 提交于 2019-12-02 19:23:55
问题 I am currently using Django-registration-redux for my authentication system. Already logged in user can visit the login and registration page again; which is not good enough. How can I prevent them from this since the views.py comes by default in Django-registration-redux I think that should be done in the views.py that come with Django-registration-redux Here is the views.py """ Views which allow users to create and activate accounts. """ from django.shortcuts import redirect from django

Intermittent 403 response from django.contrib.auth.views.login()

两盒软妹~` 提交于 2019-12-02 18:36:04
问题 Using django.contrib.auth.views.login() to process user logins I'm seeing 403 responses in a production environment. A second attempt to login succeeds after an initial 403 (when that response occurs). I've begun to log all 403 login failures, capturing the POST payload and cookie values which shows that csrfmiddlewaretoken (the hidden form field value) and csrftoken (cookie value) don't match. It's intermittent and happens to many users. The following decorators are all applied to the login

How to pass a product download link inside the django template

梦想与她 提交于 2019-12-02 18:32:00
问题 how do i pass the link to download a product if the particular product is downloadable models.py protected_loc = settings.PROTECTED_UPLOADS def download_loc(instance,filename): return "%s/%s" %(instance.slug, filename) # if instance.user.username: # return "%s/download/%s" %(instance.user.username,filename) # else: # return "%s/download/%s" %("default",filename) class Product(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True) name =

How do you use get_context_data with TemplateView in Django [closed]

删除回忆录丶 提交于 2019-12-02 17:55:09
I'm trying to do something like this: class AboutView(TemplateView): template_name = 'about.html' def get_context_data(self, **kwargs): context = super(AboutView, self).get_context_data(**kwargs) context['dahl_books'] = Books.objects.filter(author="Dahl') When I try to access dahl_books in my template like this: {% for book in dahl_books %} dahl_books is not available in the template context, even though the Books QuerySet returned a non-zero number of books. ....am I doing something wrong in either my template or in get_context_data ? I can't test it, but I bet you need return context at the

Django DoesNotExist

泄露秘密 提交于 2019-12-02 17:48:37
I am having issues on trying to figure "DoesNotExist Errors", I have tried to find the right way for manage the no answer results, however I continue having issues on "DoesNotExist" or "Object hast not Attribute DoestNotExists" from django.http import HttpResponse from django.contrib.sites.models import Site from django.utils import simplejson from vehicles.models import * from gpstracking.models import * def request_statuses(request): data = [] vehicles = Vehicle.objects.filter() Vehicle.vehicledevice_ for vehicle in vehicles: try: vehicledevice = vehicle.vehicledevice_set.get(is_joined_

AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context

邮差的信 提交于 2019-12-02 16:44:43
I want to create a many-to-many relationship where one person can be in many clubs and one club can have many persons. I added the models.py and serializers.py for the following logic but when I try to serialize it in the command prompt, I get the following error - What am I doing wrong here? I don't even have a HyperlinkedIdentityField Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Users\user\corr\lib\site-packages\rest_framework\serializers.py", line 503, in data ret = super(Serializer, self).data File "C:\Users\user\corr\lib\site-packages\rest_framework

Django request get parameters

坚强是说给别人听的谎言 提交于 2019-12-02 16:28:28
In a Django request I have the following: POST:<QueryDict: {u'section': [u'39'], u'MAINS': [u'137']}> How do I get the values of section and MAINS ? if request.method == 'GET': qd = request.GET elif request.method == 'POST': qd = request.POST section_id = qd.__getitem__('section') or getlist.... Johannes Gorset You can use [] to extract values from a QueryDict object like you would any ordinary dictionary. # HTTP POST variables request.POST['section'] # => [39] request.POST['MAINS'] # => [137] # HTTP GET variables request.GET['section'] # => [39] request.GET['MAINS'] # => [137] # HTTP POST and