django-views

Django search fields in multiple models

本秂侑毒 提交于 2019-12-03 06:25:20
问题 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

Django - Getting last object created, simultaneous filters

删除回忆录丶 提交于 2019-12-03 05:28:21
问题 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)

Django test not loading fixture data

扶醉桌前 提交于 2019-12-03 05:02:13
I have written tests for a Django project that i am working on, but one particular fixture fails to load. The fixture is generated using dumpdata and i havent fiddled with it at all. I can load the data using manage.py on that fixture without errors. I have verified that the data actually loaded using shell and querying the data. This is driving me nuts, any help would be much appreciated. Here is my test file (irrelevant portions removed): class ViewsFromUrls(TestCase): fixtures = [ 'centers/fixtures/test_data.json', 'intranet/fixtures/test_data.json', 'training/fixtures/test_data.json', #The

In Django how can I create a user and a user profile at the same time from a single form submission

大兔子大兔子 提交于 2019-12-03 04:56:14
I am using extended version of the UserCreationForm to add users via my own template, which is working well. I would also like to include, as part of the same form template, a custom field from my userprofile model, so that when the user is created a user profile with my custom field would also be created. My approach to this has been to use two forms and combine them in one template with a single submit button. The form displays exactly as I wanted, and returns validation errors correctly, but predictably it falls down when it comes to saving to the database. When I call save() on the user

Test Django views that require login using RequestFactory

我的梦境 提交于 2019-12-03 04:45:52
问题 I'm new to Django and I'd like to unit test a view that requires the user to be logged in ( @login_requred ). Django kindly provides the RequestFactory , which I can theoretically use to call the view directly: factory = RequestFactory() request = factory.get("/my/home/url") response = views.home(request) However, the call fails with AttributeError: 'WSGIRequest' object has no attribute 'session' Apparently, this is intentional, but where does that leave me? How do I test views that require

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

二次信任 提交于 2019-12-03 04:44:02
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . 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

Django DoesNotExist

谁说胖子不能爱 提交于 2019-12-03 04:40:36
问题 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

In django do models have a default timestamp field?

旧街凉风 提交于 2019-12-03 04:06:27
问题 In django - is there a default timestamp field for all objects? That is, do I have to explicitly declare a 'timestamp' field for 'created on' in my Model - or is there a way to get this automagically? 回答1: No such thing by default, but adding one is super-easy. Just use the auto_now_add parameter in the DateTimeField class: created = models.DateTimeField(auto_now_add=True) You can also use auto_now for an 'updated on' field. Check the behavior of auto_now here. For auto_now_add here. A model

Trying to use django and dropzone/

冷暖自知 提交于 2019-12-03 03:47:09
I'm trying to use dropzone.js with django. I'm following the somewhat dated guide here ( https://amatellanes.wordpress.com/2013/11/05/dropzonejs-django-how-to-build-a-file-upload-form/ ) I strongly suspect My view is at issue. def test(request): print "test view has been called" if request.method == 'POST': print "test request method is POST" form = UploadFileForm(request.POST, request.FILES) print request print request.FILES if form.is_valid(): new_file = AttachedFiles(attachedfile=request.FILES['file']) new_file.save() id = new_file.pk print id print "test form valid" return HttpResponse

How do I create list and detail views for django-taggit?

柔情痞子 提交于 2019-12-03 01:36:50
I have a fairly simple model that uses Django Taggit for tagging. Everything works great, but now I'd like to expand some functionality and I'm a little confused. What I want is two views. One that shows all my tags in the system. One that shows all the content from my app with a specific tag. What makes sense to me is to do the following for each view. in views.py for myapp All Tags from myapp.models import App from taggit.models import Tag class TagList(ListView): """ Get all the tags in the db """ queryset = Tag.objects.all() template_name = "myapp/TagList.html" paginate_by = 10 All content