django-views

Django reverse ordering with ListView

血红的双手。 提交于 2019-12-03 21:07:47
I have implemented ordering in a generic ListView: class CarList(LoginRequiredMixin, ListView): model = Car paginate_by = 30 ordering = 'car_id_internal' def get_ordering(self): return self.request.GET.get('ordering', 'car_id_internal') def get_context_data(self, *args, **kwargs): context = super(CarList, self).get_context_data(*args, **kwargs) context['current_order'] = self.get_ordering() return context And in my template: <thead> <tr> <th><a href="{% url 'car_list' %}?ordering=car_id_internal">Internal car ID</a></th> <th><a href="{% url 'car_list' %}?ordering=type">Type</a></th> <th><a

Django best practice with foreign key queries

馋奶兔 提交于 2019-12-03 17:16:42
models.py class Category(models.Model): name = models.CharField(max_length=50) class SubCatergory(models.Model): parent_category = models.ForeignKey(Category) name = models.CharField(max_length=100) views.py def all_products(request): c = Category.objects.all() s = SubCatergory.objects.all() return render_to_response('all_products.html', {'c':c, 's':s}) all_products.html {% for category in c %} <h1>{{ category.name }}</h1> <ul> {% for sub in s %} {% if category.id == sub.parent_category.id %} <li>{{ sub.name }}</li> {% endif %} {% endfor %} </ul> {% endfor %} Just wondering if above is best

How to Create Session Variables in Selenium/Django Unit Test?

女生的网名这么多〃 提交于 2019-12-03 16:27:38
I'm trying to write a functional test that uses Selenium to test a Django view. When the user comes to a page ("page2"), the view that renders that page expects to find a session variable "uid" (user ID). I've read a half dozen articles on how this is supposed to be done but none of them have worked for me. The code below shows how the Django documentation says it should be done but it doesn't work for me either. When I run the test, the view never completes executing and I get a "server error occurred" message. Could someone please tell me what I'm doing wrong? Thank you. views.py: from

'CheckoutView' object has no attribute 'object'

若如初见. 提交于 2019-12-03 15:24:42
I am getting no attribute 'object' error' here is views.py class CheckoutView(FormMixin , DetailView): model = Cart template_name = "carts/checkout_view.html" form_class = GuestCheckoutForm def get_object(self , *args , **kwargs): if self.request.user.is_authenticated(): try: cart = Cart.objects.get(user__username=self.request.user) except: cart = None if cart == None: HttpResponseRedirect(reverse("cart")) else: cart_id = self.request.session.get("cart_id") if cart_id == None: HttpResponseRedirect(reverse("cart")) cart = Cart.objects.get(id=cart_id) return cart def get_context_data(self ,*args

Django test not loading fixture data

百般思念 提交于 2019-12-03 15:23:22
问题 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

Django - short non-linear non-predictable ID in the URL

五迷三道 提交于 2019-12-03 14:44:40
I know there are similar questions (like this , this , this and this ) but I have specific requirements and looking for a less-expensive way to do the following (on Django 1.10.2): Looking to not have sequential/guessable integer ids in the URLs and ideally meet the following requirements: Avoid UUIDs since that makes the URL really long. Avoid a custom primary key. It doesn’t seem to work well if the models have ManyToManyFields. Got affected by at least three bugs while trying that ( #25012 , #24030 and #22997 ), including messing up the migrations and having to delete the entire db and

Getting 'str' object has no attribute 'get' in Django

我与影子孤独终老i 提交于 2019-12-03 14:33:40
问题 views.py def generate_xml(request, number): caller_id = 'x-x-x-x' resp = twilio.twiml.Response() with resp.dial(callerId=caller_id) as r: if number and re.search('[\d\(\)\- \+]+$', number): r.number(number) else: r.client('test') return str(resp) url.py url(r'^voice/(?P<number>\w+)$', 'django_calling.views.generate_xml', name='generating TwiML'), Whenever I am requesting http://127.0.0.1:8000/voice/number?id=98 getting following error: Request Method: GET Request URL: http://127.0.0.1:8000

Multiple models generic ListView to template

二次信任 提交于 2019-12-03 14:28:20
问题 What is the SIMPLEST method for getting 2 models to be listed in a generic IndexView? My two models are CharacterSeries and CharacterUniverse . My views.py from .models import CharacterSeries, CharacterUniverse class IndexView(generic.ListView): template_name = 'character/index.html' context_object_name = 'character_series_list' def get_queryset(self): return CharacterSeries.objects.order_by('name') class IndexView(generic.ListView): template_name = 'character/index.html' context_object_name

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

谁说我不能喝 提交于 2019-12-03 13:50:23
问题 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,

Get Django views.py to return and execute javascript

女生的网名这么多〃 提交于 2019-12-03 13:42:05
So I'm working with django and file uploads and I need a javascript function to execute after the file has been uploaded. I have a file upload handler in my views.py which looks like this: def upload_file(request): form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): for f in request.FILES.getlist('fileAttachments'): handle_uploaded_file(f) return HttpJavascriptResponse('parent.Response_OK();') else: return HttpResponse("Failed to upload attachment.") And I found a django snippet from http://djangosnippets.org/snippets/341/ and I put the HttpJavascriptResponse class in my