django-views

Getting a ValueError: invalid literal for int() with base 10: '' error and don't know why

可紊 提交于 2019-12-10 23:07:57
问题 I know this has been asked before, but for my circumstance, I can't seem to figure out why this is being thrown When I try to run my calculations, I am given this error from my console: ValueError: invalid literal for int() with base 10: '' and it is saying its coming from File "/var/sites/live_mbpathways/moneybroker/apps/investments/ajax.py", line 30, in calc_cdic investments = Investment.objects.all().filter(plan = plan, financial_institution=fiid, maturity_date__gte = now).order_by(

How To Properly Customize Django LoginView

随声附和 提交于 2019-12-10 20:42:32
问题 I am trying to figure out how to customize the django LoginView based on whether or not it's the user's first time logging on for the day. I have my LoginView currently set up so that it defaults to the LOGIN_REDIRECT_URL = "book:author" in my settings.py file. This works flawlessly. When a user logins in and is successfully authenticated, they are redirected to "book:author" as I would expect. What I'm trying to do is if this is the first time the user has logged in for the day, direct them

Django - how to save my hashed password

旧城冷巷雨未停 提交于 2019-12-10 20:17:02
问题 I'm trying to save my hashed password in my database, but It keeps saving my plaintext password Models: class StudentRegistration(models.Model): email = models.EmailField(max_length=50) first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20) password = models.CharField(max_length=100, default="", null=False) prom_code = models.CharField(max_length=8, default="", null=False) gender = ( ("M","Male"), ("F","Female"), ) gender = models.CharField(max_length=1,

Use of full-text search + GIN in a view (Django 1.11 )

Deadly 提交于 2019-12-10 18:53:33
问题 I need some help with building proper query in a django view for full-text search using GIN index. I have quite a big database (~400k lines) and need to do a full-text search on 3 fields from it. Tried to use django docs search and this is code BEFORE GIN. It works, but takes 6+ seconds to search over all fields. Next I tried to implement a GIN index to speed up my search. There are a lot of questions already how to build it. But my question is - how does the view query change when using a

Detect Decorator in Python

一个人想着一个人 提交于 2019-12-10 18:52:25
问题 In python, is it possible to detect if there is a decorator on another function? Specifically, I'm trying (in django) to write some middleware that will detect if the view being processed has been wrapped in the @login_required decorator. class SomeMiddleware(object): def process_view(self, request, view_func, view_args, view_kwargs): if has_decorator(view_func): print "this view was decorated" What I'm trying to fill in is the "has_decorator" portion.... Is this possible? 回答1: Just some

File downloaded always blank in Python, Django

感情迁移 提交于 2019-12-10 18:18:06
问题 I am using the following view in Django to create a file and make the browser download it def aux_pizarra(request): myfile = StringIO.StringIO() myfile.write("hello") response = HttpResponse(FileWrapper(myfile), content_type='text/plain') response['Content-Disposition'] = 'attachment; filename=prueba.txt' return response But the file downloaded is always blank. Any ideas? Thanks 回答1: You have to move the pointer to the beginning of the buffer with seek and use flush just in case the writing

what its the best way to make full history for a model in django?

情到浓时终转凉″ 提交于 2019-12-10 18:04:37
问题 How to make history for a model that show me the old value and the new changed value in a field? In my mind i think to make history model to each model which have the same fields and when the user create new or update the data from the first model go to the second model too. I searched alot for this problem and i found a packet called django-reversion but i didn't understand it how to make it store the old value and new value if any one can help me for this problem please tell me. 回答1: For

Decorating all django admin views (1.4+)

北慕城南 提交于 2019-12-10 17:28:31
问题 There used to be a neat trick for Django versions prior to 1.4 to decorate all views within the admin: urlpatterns = patterns('', (r'^admin/(.*)', my_decorator(lambda *args: admin.site.root(*args))), ) This no longer works as root is deprecated. I have found some alternatives, but they seem pretty verbose compared to what I had. Is there still a hook to do this? 回答1: Decorate every view in a url tree http://djangosnippets.org/snippets/2607/ 来源: https://stackoverflow.com/questions/14537829

Simulating a POST request in Django

戏子无情 提交于 2019-12-10 17:05:08
问题 Let's suppose I have the following url: /valid/django/app/path/?foo=bar&spam=eggs I can simulate a request to this URL in Django thusly: from django.shortcuts import render from django.core.urlresolvers import resolve def simulate(request, url=None, template_name="not_important.html"): if url: dv = resolve(url.split('?')[0]) return dv.func(request, *dv.args, **dv.kwargs) else: return render(request, template_name) However, I'd like to include the parameters to the included view, so that the

How to fix a Deprecation Warning in Django 1.9

家住魔仙堡 提交于 2019-12-10 16:39:07
问题 I am a new user of the Django Framework. I am currently building a REST API with the django_rest_framework. When starting my server I am getting deprecation warnings that I have no idea how to fix. RemovedInDjango110Warning: 'get_all_related_objects is an unofficial API that has been deprecated. You may be able to replace it with 'get_fields()' for relation in opts.get_all_related_objects() The above is the first of these. Does anyone know how to fix this issue. All I have in my API at the