django-views

How to handle “matching query does not exist” when getting an object

半腔热情 提交于 2019-11-27 16:09:11
When I want to select objects with a get() function like personalProfile = World.objects.get(ID=personID) If get function doesn't return find a value, a "matching query does not exist." error occurs. If I don't need this error, I'll use try and except function try: personalProfile = World.objects.get(ID=personID) except: pass But I think this is not the best way since I use except: pass Please recommend some idea or code sample to fight with this issue That depends on what you want to do if it doesn't exist.. Theres get_object_or_404 : Calls get() on a given model manager, but it raises

Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found

流过昼夜 提交于 2019-11-27 14:43:41
问题 I'm following the official tutorial to learn Django and using 1.5. I had this link as part of my index template, which was working fine: <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> however, this is hardcoded and the tutorial suggested a better way was to use: <li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li> so that you'll be better of when dealing with huge number of templates and u have to make changes to the url. Since I made the above change I get

Django - show loading message during long processing

筅森魡賤 提交于 2019-11-27 14:31:10
How can I show a please wait loading message from a django view? I have a Django view that takes significant time to perform calculations on a large dataset. While the process loads, I would like to present the user with a feedback message e.g.: spinning loading animated gif or similar. Evan Davey After trying the two different approaches suggested by Brandon and Murat, Brandon's suggestion proved the most successful. Create a wrapper template that includes the javascript from http://djangosnippets.org/snippets/679/ . The javascript has been modified: (i) to work without a form (ii) to hide

CSRF verification failed. Request aborted. on django

。_饼干妹妹 提交于 2019-11-27 14:16:52
I am following Django 1.3 Web Development. and for logins, i am getting the following error Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. This is my settings.py Included APPS. It is exactly how the book says it should be. INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin

django accessing raw many to many created table fields

穿精又带淫゛_ 提交于 2019-11-27 14:06:12
问题 Model: class Subjects (models.Model): name = models.CharField(max_length=100) places = models.CharField(max_length=100) class Student (models.Model): name = models.CharField(max_length=40) lastname = models.CharField(max_length=80) subjects = models.ManyToManyField(Subjects, blank=True) Django creates appname_student_subjects when I use model above. appname_student_subjects table looks for example, like this: id | student_id | subjects_id ----------------------------------------- 1 | 1 | 10 2

Display objects from different models at the same page according to their published date

自闭症网瘾萝莉.ら 提交于 2019-11-27 14:06:05
I have three different models for my app. All are working as I expected. class Tender(models.Model): title = models.CharField(max_length=256) description = models.TextField() department = models.CharField(max_length=50) address = models.CharField(max_length=50) nature_of_work = models.CharField(choices=WORK_NATURE, max_length=1) period_of_completion = models.DateField() pubdat = models.DateTimeField(default=timezone.now) class Job(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL) title = models.CharField(max_length=256) qualification = models.CharField(max_length=256) interview

Add data to ModelForm object before saving

不想你离开。 提交于 2019-11-27 13:34:59
问题 Say I have a form that looks like this: forms.py class CreateASomethingForm(ModelForm): class Meta: model = Something fields = ['field2', 'field3', 'field4'] I want the form to have these three fields. However my Something class also has field1 . My question is - how do I add data to field1 , if I am not using the ModelForm to collect the data. I tried doing something like this, but it isn't working and I am unsure on the proper way to solve this: views.py def create_something_view(request):

Django class based views - UpdateView with two model forms - one submit

被刻印的时光 ゝ 提交于 2019-11-27 13:19:56
问题 I have a page with a list of users, and would like to be able to click a link to update their profile. When 'update' is clicked, I should be able to edit the username, first name, ... email, phone number, department etc, in a single page, using a single submit button. I accomplished this by using two forms, one for User, and one for the extra information. The ListView, DeleteView and CreateView work perfectly with these two forms, but not the UpdateView. I am not able to instantiate the two

Execute code in Django after response has been sent to the client

随声附和 提交于 2019-11-27 13:15:31
In my Django application I want to keep track of whether a response has been sent to the client successfully. I am well aware that there is no "watertight" way in a connectionless protocol like HTTP to ensure the client has received (and displayed) a response, so this will not be mission-critical functionality, but still I want to do this at the latest possible time. The response will not be HTML so any callbacks from the client (using Javascript or IMG tags etc.) are not possible. The "latest" hook I can find would be adding a custom middleware implementing process_response at the first

Refresh div using JQuery in Django while using the template system

混江龙づ霸主 提交于 2019-11-27 12:52:45
问题 I want to refresh a div tag in Django that contains temperature data. The data is fetched every 20 seconds. So far I have achieved this using these functions: function refresh() { $.ajax({ url: '{% url monitor-test %}', success: function(data) { $('#test').html(data); } }); }; $(function(){ refresh(); var int = setInterval("refresh()", 10000); }); And this is my urls.py: urlpatterns += patterns('toolbox.monitor.views', url(r'^monitor-test/$', 'temperature', name="monitor-test"), url(r'