Re: how to use "{% url *** %}" in django template file?
You can add name to the url in the urlpatterns: urlpatterns = patterns('', url(r'card/create$', 'card.views.create_card', name='card_create_card'), ) and call it in the template: {% url card_create_card %} On Aug 7, 1:05 pm, Jimmy wrote: > Hi, > > I got the error "Caught ImportError while rendering: No module named > urls" when using: > > {% url 'card.views.create_card' %} in the template file > > in the urls.py the route to the url is: > > urlpatterns = patterns('', > url(r'card/create$', 'card.views.create_card'), > ) > > The Django version I use is 1.3 > > May I know what did I miss in setting? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Seperate Image Model
Maybe I should add I don't want to manually assign the images, but script this as much as possible. It involves 1000s of images. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/9xUVzEDBtWEJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Seperate Image Model
The images and creation of thumbnails aren't the problem. I'll look into the 1th and 3rd. I've looked at a lot of image apps that might give a solution to my problem, but none fitted my requirements. I'm not sure how to get the images in the content. There might be no image at all, but also one or more. I've also been thinking about using markdown or creating an additional textfield in the model. Maybe I can put the img.id or img.filename in there (e.g. *some text* [[img.id, width=100, etc]] *and some more text* ) -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Xx8NX2LV0p4J. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Problem with django book in Forms chapter 7
Check the comments on LHS of the Book page. In there is a simple method that works and does not need to remove the middleware. Specifically (as there are loads of comments) Frank Kruchio's comment in the comment section next to this text Try running this locally. Load the form, submit it with none of the fields filled out, submit it with an invalid e-mail address, then finally submit it with valid data. (Of course, depending on your mail-server configuration, you might get an error when send_mail() is called, but that’s another issue.) On 8/8/2011 8:57 AM, Hayyan Rafiq wrote: Here is how i did it @csrf_exempt def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data send_mail( cd['subject'], cd['message'], cd.get('email', 'nore...@example.com'), ['siteow...@example.com'], ) return HttpResponseRedirect('/contact/thanks/') else: form = ContactForm() return render_to_response('contact_form.html', {'form': form}) From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: RE: Problem with django book in Forms chapter 7 Date: Sun, 7 Aug 2011 20:11:51 + Hi just started facing the same problem which you did in chapter 7 . I tried using def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data send_mail( cd['subject'], cd['message'], cd.get('email', 'nore...@example.com'), ['siteow...@example.com'], ) return HttpResponseRedirect('/contact/thanks/') else: form = ContactForm() return render_to_response('contact_form.html', {'form': form},context_instance=RequestContext(request)) but still i get the following could you please tell me how you resolved the issue... Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: The view function uses RequestContext for the template, instead of Context. In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting. > Date: Sun, 7 Aug 2011 09:10:36 +0200 > From: rafadurancastan...@gmail.com > To: django-users@googlegroups.com > Subject: Re: Problem with django book in Forms chapter 7 > > I had the same problem as you, since the book was written using an older > django version and there was some changes on csrf for django version > 1.2. Looking at django docs > https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#how-to-use-it > you can read recommended way to use this > > > On 06/08/11 23:05, bob gailer wrote: > > I love the django book. Until I got to the section "Tying Your First > > Form Class". > > > > Problem:-"This class can live anywhere you
Re: Seperate Image Model
Perhaps one of these: http://schbank.wordpress.com/2010/09/28/django-application-a-simple-gallery/ http://djangopackages.com/grids/g/gallery/ http://gitorious.org/django-simple-gallery/django-simple-gallery/trees/master On 8/7/2011 11:33 PM, Josh wrote: I'm only working a few weeks with Django and I want some advice about handling images. Conceptual I have to remind myself that Django is pure python, so that makes me complicate things sometimes. Also I still have some conceptual difficulties with templates and template_tags. Also I'm not really a programmer or webdesigner. I'll just describe my approach and finish with my problems. In my models I want to use a seperate model for Images that are displayed with content, because content might have more than one image connected to them. The images can be local on a mediaserver or taken from an external URL. class Image(models.Model): image = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, unique=True, blank=True, null=True,) thumbnail = models.ImageField(upload_to=None, height_field=80, width_field=120, max_length=100, unique=True, blank=True, null=True,) urlimage = models.URLField(verify_exists=True, max_length=200, blank=True, null=True,) smallthumbnail = models.ImageField(upload_to=None, height_field=40, width_field=60, max_length=100, unique=True, blank=True, null=True,) entry = models.ManyToManyField(Entry, blank=True, null=True, default = None) urlexists = modelf.IntegerField(default=0) def save(self, force_insert=False, force_update=False): if self.urlimage.verify_exists == True: #test to check: 0 = False, 1 = True self.urlexists = 1 ... Images will be uploaded and thumbnailed beforehand. The images on the mediaserver have the same name and are split in 'full/', 'smallthumbnail/' and 'thumbnail/' directories. When displaying the content I'll retrieve the corresponding images and recreate the url using {{ MEDIA_URL }} and the respective directories. The html will be written in the template. My problem is what the best way to achieve this? My solution is below in (pseudo) code. I want to use get_absolute_url to achieve this. @models.permalink def get_absolute_url(self): if self.urlimage.verify_exists == True: return ('templatename', (), { 'urlimage': self.urlimage, } ) elif len(self.image) > 0: #check if return ('template_name', (), { 'image': self.image, 'thumbnail': self.thumbnail, 'smallthumbnail': self.smallthumbnail } ) else: #Can this check be used to delete the record from the database and how do I do this? remove-from-table return None Am I complicating things or are there better alternatives to achieve this? -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/hC6kwkOXbncJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Seperate Image Model
consider http://code.google.com/p/django-photologue/ On 8/7/2011 11:33 PM, Josh wrote: I'm only working a few weeks with Django and I want some advice about handling images. Conceptual I have to remind myself that Django is pure python, so that makes me complicate things sometimes. Also I still have some conceptual difficulties with templates and template_tags. Also I'm not really a programmer or webdesigner. I'll just describe my approach and finish with my problems. In my models I want to use a seperate model for Images that are displayed with content, because content might have more than one image connected to them. The images can be local on a mediaserver or taken from an external URL. class Image(models.Model): image = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, unique=True, blank=True, null=True,) thumbnail = models.ImageField(upload_to=None, height_field=80, width_field=120, max_length=100, unique=True, blank=True, null=True,) urlimage = models.URLField(verify_exists=True, max_length=200, blank=True, null=True,) smallthumbnail = models.ImageField(upload_to=None, height_field=40, width_field=60, max_length=100, unique=True, blank=True, null=True,) entry = models.ManyToManyField(Entry, blank=True, null=True, default = None) urlexists = modelf.IntegerField(default=0) def save(self, force_insert=False, force_update=False): if self.urlimage.verify_exists == True: #test to check: 0 = False, 1 = True self.urlexists = 1 ... Images will be uploaded and thumbnailed beforehand. The images on the mediaserver have the same name and are split in 'full/', 'smallthumbnail/' and 'thumbnail/' directories. When displaying the content I'll retrieve the corresponding images and recreate the url using {{ MEDIA_URL }} and the respective directories. The html will be written in the template. My problem is what the best way to achieve this? My solution is below in (pseudo) code. I want to use get_absolute_url to achieve this. @models.permalink def get_absolute_url(self): if self.urlimage.verify_exists == True: return ('templatename', (), { 'urlimage': self.urlimage, } ) elif len(self.image) > 0: #check if return ('template_name', (), { 'image': self.image, 'thumbnail': self.thumbnail, 'smallthumbnail': self.smallthumbnail } ) else: #Can this check be used to delete the record from the database and how do I do this? remove-from-table return None Am I complicating things or are there better alternatives to achieve this? -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/hC6kwkOXbncJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
RE: Problem with django book in Forms chapter 7
Here is how i did it @csrf_exempt def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data send_mail( cd['subject'], cd['message'], cd.get('email', 'nore...@example.com'), ['siteow...@example.com'], ) return HttpResponseRedirect('/contact/thanks/') else: form = ContactForm() return render_to_response('contact_form.html', {'form': form}) From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: RE: Problem with django book in Forms chapter 7 Date: Sun, 7 Aug 2011 20:11:51 + Hi just started facing the same problem which you did in chapter 7 . I tried using def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data send_mail( cd['subject'], cd['message'], cd.get('email', 'nore...@example.com'), ['siteow...@example.com'], ) return HttpResponseRedirect('/contact/thanks/') else: form = ContactForm() return render_to_response('contact_form.html', {'form': form},context_instance=RequestContext(request)) but still i get the following could you please tell me how you resolved the issue... Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: The view function uses RequestContext for the template, instead of Context.In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting. > Date: Sun, 7 Aug 2011 09:10:36 +0200 > From: rafadurancastan...@gmail.com > To: django-users@googlegroups.com > Subject: Re: Problem with django book in Forms chapter 7 > > I had the same problem as you, since the book was written using an older > django version and there was some changes on csrf for django version > 1.2. Looking at django docs > https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#how-to-use-it > you can read recommended way to use this > > > On 06/08/11 23:05, bob gailer wrote: > > I love the django book. Until I got to the section "Tying Your First > > Form Class". > > > > Problem:-"This class can live anywhere you want — including directly > > in your views.py file — but community convention is to keep Form > > classes in a separate file called forms.py. Create this file in the > > same directory as your views.py" The examples then use from > > contact.forms import ContactForm. Where did contact come from? I had > > to remove it to get the import to work! > > > > Then all is OK until "Tying Form Objects Into Views". Here is where I > > run into the > > CSRF verification failed. Request aborted. > > Reason given for failure:CSRF token missing or incorrect." > > > > After much searching I found: > > > > from django.template import RequestContext > > ... > > form = ContactForm() > > return render_to_response('contact_form.html', {'form': form}, > > > > context_instance=RequestContext(request)) > > and now it works. > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@
RE: Problem with django book in Forms chapter 7
Hi just started facing the same problem which you did in chapter 7 . I tried using def contact(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): cd = form.cleaned_data send_mail( cd['subject'], cd['message'], cd.get('email', 'nore...@example.com'), ['siteow...@example.com'], ) return HttpResponseRedirect('/contact/thanks/') else: form = ContactForm() return render_to_response('contact_form.html', {'form': form},context_instance=RequestContext(request)) but still i get the following could you please tell me how you resolved the issue... Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: The view function uses RequestContext for the template, instead of Context.In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting. > Date: Sun, 7 Aug 2011 09:10:36 +0200 > From: rafadurancastan...@gmail.com > To: django-users@googlegroups.com > Subject: Re: Problem with django book in Forms chapter 7 > > I had the same problem as you, since the book was written using an older > django version and there was some changes on csrf for django version > 1.2. Looking at django docs > https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#how-to-use-it > you can read recommended way to use this > > > On 06/08/11 23:05, bob gailer wrote: > > I love the django book. Until I got to the section "Tying Your First > > Form Class". > > > > Problem:-"This class can live anywhere you want — including directly > > in your views.py file — but community convention is to keep Form > > classes in a separate file called forms.py. Create this file in the > > same directory as your views.py" The examples then use from > > contact.forms import ContactForm. Where did contact come from? I had > > to remove it to get the import to work! > > > > Then all is OK until "Tying Form Objects Into Views". Here is where I > > run into the > > CSRF verification failed. Request aborted. > > Reason given for failure:CSRF token missing or incorrect." > > > > After much searching I found: > > > > from django.template import RequestContext > > ... > > form = ContactForm() > > return render_to_response('contact_form.html', {'form': form}, > > > > context_instance=RequestContext(request)) > > and now it works. > > > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Why isnt this simple import working??
I think if you should read http://docs.python.org/tutorial/modules.html, specially packages part On 07/08/11 21:51, Hayyan Rafiq wrote: Got it working just inserted a init.py file init. so it got recognized as a python directory From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: RE: Why isnt this simple import working?? Date: Sun, 7 Aug 2011 19:48:03 + I think i know why this isnt working becuase the form folder does not have an init.py init.py init. So my qustion is how do u import a class from a python file in a random folder? From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: Why isnt this simple import working?? Date: Sun, 7 Aug 2011 19:35:30 + While trying to construct an example from the django book chapter7. I used the following import code in my views.py *from mysite.form.forms import ContactForm* now ContactForm is a class in forms.py located in "D:\Django-1.3\django\bin\mysite\form" where mysite=dir form=dir forms= python file ContactForm =Class Ultimately i get this error ImportError at /contact No module named form.forms Request Method: GET Request URL:http://127.0.0.1:8000/contact Django Version: 1.3 Exception Type: ImportError Exception Value: No module named form.forms Exception Location: D:\Django-1.3\django\bin\mysite\views.py in , line 4 Python Executable: D:\Python27\python.exe Python Version: 2.7.2 Python Path: ['D:\\Django-1.3\\django\\bin\\mysite', 'D:\\Python27\\Lib\\site-packages', '*D:\\Django-1.3\\django\\bin*', 'C:\\WINDOWS\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27'] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
RE: Why isnt this simple import working??
Got it working just inserted a init.py file init. so it got recognized as a python directory From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: RE: Why isnt this simple import working?? Date: Sun, 7 Aug 2011 19:48:03 + I think i know why this isnt working becuase the form folder does not have an init.py init.py init. So my qustion is how do u import a class from a python file in a random folder? From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: Why isnt this simple import working?? Date: Sun, 7 Aug 2011 19:35:30 + While trying to construct an example from the django book chapter7. I used the following import code in my views.py from mysite.form.forms import ContactForm now ContactForm is a class in forms.py located in "D:\Django-1.3\django\bin\mysite\form" where mysite=dir form=dir forms= python file ContactForm =Class Ultimately i get this error ImportError at /contact No module named form.forms Request Method: GET Request URL: http://127.0.0.1:8000/contact Django Version: 1.3 Exception Type: ImportError Exception Value: No module named form.forms Exception Location: D:\Django-1.3\django\bin\mysite\views.py in , line 4 Python Executable: D:\Python27\python.exe Python Version: 2.7.2 Python Path: ['D:\\Django-1.3\\django\\bin\\mysite', 'D:\\Python27\\Lib\\site-packages', 'D:\\Django-1.3\\django\\bin', 'C:\\WINDOWS\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27'] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
RE: Why isnt this simple import working??
I think i know why this isnt working becuase the form folder does not have an init.py init.py init. So my qustion is how do u import a class from a python file in a random folder? From: hayya...@hotmail.com To: django-users@googlegroups.com Subject: Why isnt this simple import working?? Date: Sun, 7 Aug 2011 19:35:30 + While trying to construct an example from the django book chapter7. I used the following import code in my views.py from mysite.form.forms import ContactForm now ContactForm is a class in forms.py located in "D:\Django-1.3\django\bin\mysite\form" where mysite=dir form=dir forms= python file ContactForm =Class Ultimately i get this error ImportError at /contact No module named form.forms Request Method: GET Request URL: http://127.0.0.1:8000/contact Django Version: 1.3 Exception Type: ImportError Exception Value: No module named form.forms Exception Location: D:\Django-1.3\django\bin\mysite\views.py in , line 4 Python Executable: D:\Python27\python.exe Python Version: 2.7.2 Python Path: ['D:\\Django-1.3\\django\\bin\\mysite', 'D:\\Python27\\Lib\\site-packages', 'D:\\Django-1.3\\django\\bin', 'C:\\WINDOWS\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27'] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Problems with prepopulated_fields JS
Python 2.6.1 Django (1, 3, 0, 'final', 0) hello I've added Category model to apps models class Category(models.Model): app_label = _('News') title = models.CharField(max_length=50) slug= models.SlugField(unique=True) description = models.TextField() In admin.py of my app: class CategoryAdmin(admin.ModelAdmin): prepopulated_fields = {"slug": ("title",)} admin.site.register(Category) But while adding Category there's no JS magic of automatically adding slug. Besides I see no errors in Firebug. What could cause the problem? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Why isnt this simple import working??
While trying to construct an example from the django book chapter7. I used the following import code in my views.py from mysite.form.forms import ContactForm now ContactForm is a class in forms.py located in "D:\Django-1.3\django\bin\mysite\form" where mysite=dir form=dir forms= python file ContactForm =Class Ultimately i get this error ImportError at /contact No module named form.forms Request Method: GET Request URL: http://127.0.0.1:8000/contact Django Version: 1.3 Exception Type: ImportError Exception Value: No module named form.forms Exception Location: D:\Django-1.3\django\bin\mysite\views.py in , line 4 Python Executable: D:\Python27\python.exe Python Version: 2.7.2 Python Path: ['D:\\Django-1.3\\django\\bin\\mysite', 'D:\\Python27\\Lib\\site-packages', 'D:\\Django-1.3\\django\\bin', 'C:\\WINDOWS\\system32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27'] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to test form values in a template? (simplified)
The validation is easy. Override the form's clean() method to do any validation which needs to check the value of more than one field. For example, if you want a text box to be required sometimes, define it as not required in the form, then check the boolean in clean() and raise a forms.ValidationError if appropriate. If you want to change which widget is being used and where it's displayed based on the checkbox then you'd have to use AJAX to make that work "live" anyway. Or maybe have two form fields, one of each type, and dynamically hide one and show the other when the checkbox is changed. You could also use your form's clean() override to assign the correct value to your form field. Example: Say you have a field named named 'reason,' and you want to make it a select box with hard-coded choices if a boolean for is True, but a free-form text field if it's False. If you have fields named reason_select and reason_text, you could use JavaScript to select the appropriate one to show based on the checkbox. Then, in form.clean(), you use the value of the checkbox to determine whether to fill self.cleaned_data['reason'] with the value from self.cleaned_data['reason_select'] or self.cleaned_data['reason_text']. I hope this helps. I think we're zeroing in on your solution. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to test form values in a template? (simplified)
It's more that I want to have different ways of displaying the same form field. I want the text field to be on the same line as the checkbox when it's an input field and below it when it's displayed as a textarea. There's also the point of changing the required setting of the same field based on if the checkbox is selected or not. I guess what I'm trying to get at is, how should I access these values to determine the display and validation. If I don't use my method I will have to test for both the boolean value and the string value 'True' instead. That's what I'm trying to avoid. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/HDa2SptnYuEJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
How to test form values in a template? (simplified)
I realize that I went from too little information, to too much information in the previous post, so this is an attempt to find a middle ground. What I'm building is the ability to have a list of checkable options, and depending on the setup for a give option it may have a text field to enter additional information. This text field can be either displayed as a single line text input or a multi-line textarea. My problem is that I want to have these different setup values available to control the validation and display of the form, but there doesn't seem a consistent way to test the values. When the form is POSTed back and isn't valid, the value out of the BoundField's value() metod for BooleanFields is a string, where it's a actual boolean on the initial creation of the page. I understand why this is, it's pulling from the POST data instead of the initial data. The way I over came this was to add a to_python() method to the BoundField class in django/forms/forms.py: def to_python(self): return self.field.to_python(self.value()) Below is what I'm doing. I'm using the new to_python() method are in the __init__ of the form, the save logic of the view, and the template at the end. I left out the CreateItemsSection() function that builds of the listItems dict because its convoluted. The listItems structure is in a form that's easily looped through in the template and the function creates (on the initial load) or inserts (on POST back) the forms of the formset. If it would be helpful I can post it but I think there should be enough information here. Is there a better way to accomplish what I'm doing with the to_python() method? # Form class # I use the self["multiLineInfo"] pattern instead of self.fields["multiLineInfo"] # because the former gives a BoundField which merges the Field and the # post/initial data class OrganizationItemForm(ModelForm): selected = forms.BooleanField(required=False) extraRequired = forms.BooleanField(required=False, widget=forms.HiddenInput) multiLineInfo = forms.BooleanField(required=False, widget=forms.HiddenInput) def __init__(self, *args, **kwargs): super(AuditModelForm, self).__init__(*args, **kwargs) if self["multiLineInfo"].to_python(): self.fields["descr"].widget = forms.Textarea(attrs={"class":"descriptionEdit"}) else: self.fields["descr"].widget = forms.TextInput(attrs={"class":"itemDescrEdit"}) self.fields["descr"].required = self["extraRequired"].to_python() and self["selected"].to_python() class Meta: model = OrganizationItem # Model classes class OrganizationItem(models.Model): organization = models.ForeignKey(Organization) item = models.ForeignKey(ListItem) descr = models.TextField("Description", blank=True) # View def organizationAdd(request): OrganizationItemFormSet = formset_factory(OrganizationItemForm) if request.method == 'POST': form = OrganizationForm(request.POST) orgItemFormSet = OrganizationItemFormSet(request.POST) if form.is_valid(): form.save() for itm in orgItemFormSet: if itm["selected"].to_python(): itm.instance.organization = form.instance if orgItemFormSet.is_valid(): SaveItems(form.instance, orgItemFormSet) redirect("orgEdit", form.instance.pk) else: form.instance.delete() listItems = CreateItemsSection(orgItemFormSet, category__organization=True) else: form = OrganizationForm() orgItemFormSet = OrganizationItemFormSet() listItems = CreateItemsSection(orgItemFormSet, "organization", category__organization=True) context = { 'title': 'New organization', 'form': form, 'listItems': listItems, 'listItemsManagementForm': orgItemFormSet.management_form, 'isNew': True} return render_to_response('organizationEdit.html', context, context_instance=RequestContext(request)) # Template {{ listItemsManagementForm }} {% for item in listItems %} {{ item.form.descr.errors }}{% if item.form.descr.errors %}{% endif %} {{ item.form.selected }} {{ item.label }} {% if item.form.extraRequired.to_python or item.descrLabel %} {% if item.form.multiLineInfo.to_python %} {% else %} – {% endif %} {{ item.descrLabel }} {% if item.form.multiLineInfo.to_python %} {% else %} {% endif %} {{ item.form.descr }} {% else %} {{ item.form.descr.as_hidden }} {% endif %}
Re: How to test form values in a template? (simplified)
Are you saying that you want to show some form inputs conditionally based upon configuration, for example for each user? If that's your goal then it's very easy to do by adding the logic in the form's __init__. Add/remove fields there and (possibly) override save() if you have to take any additional action. There's no need to do things like start modifying how the guts of forms.Form works for something like this. Shawn -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
How to use django-tables with Paginator
This question is related to django-tables. I hope I am allowed to post here. I will appreciate some help on how to use Django Paginator with django-tables. Below is what I have been working with: report.py import django_tables as tables from webapp.models import Transaction class TransactionReport(tables.ModelTable): #paid = tables.Column(filter='paid') tpin = tables.Column(sortable=False, visible=False) identifier = tables.Column(sortable=False, visible=False) created = tables.Column(sortable=True, visible=True) @classmethod def get_reports_paid(self, object, req): return TransactionReport(object, order_by=req) class Meta: model = Transaction views.py --- @login_required def display_reports(request): #data = Transaction.objects.all() logger = logging.getLogger(__name__) dataqs = Transaction.objects.filter(paid="TRUE") req = request.GET.get('sort', 'created') tx = TransactionReport().get_reports_paid(dataqs, req) # need to just simply pass the paginator parameter to get_reports_paid paginator = Paginator(tx, 2) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 # If page request () is out of range, deliver last page of results. try: transaction = paginator.page(page) except (EmptyPage, InvalidPage): transaction = paginator.page(paginator.num_pages) return render_to_response('webapp/reports.html', {'table': tx, 'paginator': transaction}) In reports.html, the table is displayed but all records are displayed at once instead of 2 rows then paginated pages. Any help will be appreciated. Thanks -- Odeyemi 'Kayode O. http://www.sinati.com. t: @charyorde -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Seperate Image Model
I'm only working a few weeks with Django and I want some advice about handling images. Conceptual I have to remind myself that Django is pure python, so that makes me complicate things sometimes. Also I still have some conceptual difficulties with templates and template_tags. Also I'm not really a programmer or webdesigner. I'll just describe my approach and finish with my problems. In my models I want to use a seperate model for Images that are displayed with content, because content might have more than one image connected to them. The images can be local on a mediaserver or taken from an external URL. class Image(models.Model): image = models.ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, unique=True, blank=True, null=True,) thumbnail = models.ImageField(upload_to=None, height_field=80, width_field=120, max_length=100, unique=True, blank=True, null=True,) urlimage = models.URLField(verify_exists=True, max_length=200, blank=True, null=True,) smallthumbnail = models.ImageField(upload_to=None, height_field=40, width_field=60, max_length=100, unique=True, blank=True, null=True,) entry = models.ManyToManyField(Entry, blank=True, null=True, default = None) urlexists = modelf.IntegerField(default=0) def save(self, force_insert=False, force_update=False): if self.urlimage.verify_exists == True: #test to check: 0 = False, 1 = True self.urlexists = 1 ... Images will be uploaded and thumbnailed beforehand. The images on the mediaserver have the same name and are split in 'full/', 'smallthumbnail/' and 'thumbnail/' directories. When displaying the content I'll retrieve the corresponding images and recreate the url using {{ MEDIA_URL }} and the respective directories. The html will be written in the template. My problem is what the best way to achieve this? My solution is below in (pseudo) code. I want to use get_absolute_url to achieve this. @models.permalink def get_absolute_url(self): if self.urlimage.verify_exists == True: return ('templatename', (), { 'urlimage': self.urlimage, } ) elif len(self.image) > 0: #check if return ('template_name', (), { 'image': self.image, 'thumbnail': self.thumbnail, 'smallthumbnail': self.smallthumbnail } ) else: #Can this check be used to delete the record from the database and how do I do this? remove-from-table return None Am I complicating things or are there better alternatives to achieve this? -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/hC6kwkOXbncJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: how to use "{% url *** %}" in django template file?
On Sun, Aug 7, 2011 at 10:35 AM, Jimmy wrote: > Hi, > > I got the error "Caught ImportError while rendering: No module named > urls" when using: > > {% url 'card.views.create_card' %} in the template file > For django 1.2 and earlier versions you should use the syntax {% url card.views.create_card %} Django 1.3 supports the forward compatible {% url 'card.views.create_card' %} syntax from the future library. You'll need to add the following line in your templates to use the future library. {% load url from future %} > > in the urls.py the route to the url is: > > urlpatterns = patterns('', > url(r'card/create$', 'card.views.create_card'), > ) > > The Django version I use is 1.3 > > May I know what did I miss in setting? > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Problem with django book in Forms chapter 7
I had the same problem as you, since the book was written using an older django version and there was some changes on csrf for django version 1.2. Looking at django docs https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#how-to-use-it you can read recommended way to use this On 06/08/11 23:05, bob gailer wrote: I love the django book. Until I got to the section "Tying Your First Form Class". Problem:-"This class can live anywhere you want — including directly in your views.py file — but community convention is to keep Form classes in a separate file called forms.py. Create this file in the same directory as your views.py" The examples then use from contact.forms import ContactForm. Where did contact come from? I had to remove it to get the import to work! Then all is OK until "Tying Form Objects Into Views". Here is where I run into the CSRF verification failed. Request aborted. Reason given for failure:CSRF token missing or incorrect." After much searching I found: from django.template import RequestContext ... form = ContactForm() return render_to_response('contact_form.html', {'form': form}, context_instance=RequestContext(request)) and now it works. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.