django-views

Django raising 404 with a message

北城以北 提交于 2019-12-30 08:41:30
问题 I like to raise 404 with some error message at different places in the script eg: Http404("some error msg: %s" %msg) So, in my urls.py I included: handler404 = Custom404.as_view() Can anyone please tell me how should I be handling the error in my views. I'm fairly new to Django, so an example would help a lot. Many thanks in advance. 回答1: In general, 404 error is "page not found" error - it should not have customizable messages, simply because it should be raised only when a page is not found

Django: Passing value from template to view

荒凉一梦 提交于 2019-12-30 03:34:08
问题 I HAD this situation: Clicking on a html submit button, I call views.stream_response which "activates" views.stream_response_generator which "activates" stream.py and return a StreamingHttpResponse and I see a progressive number every second up to m at /stream_response/ : 1 2 3 4 5 6 7 8 //e.g. my default max value for m stream.py from django.template import Context, Template import time def streamx(m): lista = [] x=0 while len(lista) < m: x = x + 1 time.sleep(1) lista.append(x) yield "<div>

The view didn't return an HttpResponse object. It returned None instead

别等时光非礼了梦想. 提交于 2019-12-30 01:36:51
问题 I have the following simple view. Why is it resulting in this error? The view auth_lifecycle.views.user_profile didn't return an HttpResponse object. It returned None instead. """Renders web pages for the user-authentication-lifecycle project.""" from django.shortcuts import render from django.template import RequestContext from django.contrib.auth import authenticate, login def user_profile(request): """Displays information unique to the logged-in user.""" user = authenticate(username=

Django view not rendering after Ajax redirect

こ雲淡風輕ζ 提交于 2019-12-29 09:12:14
问题 The main page of my website has multiple buttons at the top. Whenever one of these buttons is pushed, a get request is sent to a django view, which is redirected and a queryset of django models is filtered and eventually displayed on the web page. I know that my ajax works because the terminal says the request is redirected properly. The function it redirects to also seems to be working, as it is quite simple and has not thrown any errors. However, my view remains the same and I'm not sure

Django syncdb error: One or more models did not validate

那年仲夏 提交于 2019-12-29 08:37:10
问题 /mysite/project4 class notes(models.Model): created_by = models.ForeignKey(User) detail = models.ForeignKey(Details) Details and User are in the same module i.e,/mysite/project1 In project1 models i have defined class User(): ...... class Details(): ...... When DB i synced there is an error saying Error: One or more models did not validate: project4: Accessor for field 'detail' clashes with related field . Add a related_name argument to the definition for 'detail'. How can this be solved..

Nested loop in Django template

橙三吉。 提交于 2019-12-29 07:01:11
问题 I can't get my head around this. I need to somehow access the object in the parent loop but I'm not sure how. Here is what I've come up with so far. I marked the problematic area in the code with XXX : Template: {% for item in ingrcat %} <h2>{{ item.name }}</h2> <ul> {% for ingr in XXX %} <li><a href="#" id="i{{ ingr.id }}">{{ ingr.name }}</a></li> {% endfor %} </ul> {% endfor %} XXX - should be a list of ingredients belonging to the ingredience category which is currently being looped

Django FileField upload is not working for me

孤者浪人 提交于 2019-12-29 05:58:07
问题 I have been scratching my head FileField. Does the FileField require a seperate process? Although my url gets saved .. but my file doesn't get uploaded ... what am i doing wrong? This is my models.py ... class OpLink(models.Model): user = models.ForeignKey(User) file = models.FileField(blank=True, null=True, upload_to="uploads") url = models.URLField(blank=True, null=True) my forms.py class OpLinkForm(ModelForm): class Meta: model = OpLink exclude = ('user') my views.py oplinkform =

How to subclass django's generic CreateView with initial data?

守給你的承諾、 提交于 2019-12-29 03:12:26
问题 I'm trying to create a dialog which uses jquery's .load() function to slurp in a rendered django form. The .load function is passed the pk of the "alert" object. Also available in the class functions are things like self.request.user so I can pre-fill those fields, shown below in the Message model (models.py): class Message(models.Model): user = models.ForeignKey(User) alert = models.ForeignKey(Alert) date = models.DateTimeField() message = models.TextField() Subclassing django's CreateView

Django - show loading message during long processing

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 04:04:07
问题 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. 回答1: 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

Django {% url %} reverse not working

流过昼夜 提交于 2019-12-25 18:28:45
问题 I have a view in a Django 1.4 project: def index(request): print reverse('menus_index') latest_menu_list = Menu.objects.all().order_by('name') return render_to_response('menus/index.html', {'latest_menu_list': latest_menu_list}) This works as expected and prints out the reversed URL which is /menus/. Inside of the index.html template (which is called by this view) I have: {% url menus_index %} Which causes a NoReverseMatch at /menus/ error. Reverse for '' with arguments '()' and keyword