(in the terminal) python manage.py syncdb On Jan 21, 8:57 am, cha <mohamma...@gmail.com> wrote: > Hello I reading this > tutorialhttps://pype.its.utexas.edu/docs/tutorial/unit6.html#answers > it's work OK But When I want improve it and add ImageField in Category > Model i have problem with ImageField It's Not uploaded > > file = models.ImageField(upload_to='img/%Y') > > see code please : > _____ > views.py > _____ > from django import forms > class CategoryForm(forms.ModelForm): > class Meta: > model = Category > > def add_category(request): > if request.method == "POST": > form = CategoryForm(request.POST, request.FILES) > ## Handle AJAX ## > if request.is_ajax(): > if form.is_valid(): > form.save() > # Get a list of Categories to return > cats = Category.objects.all().order_by('name') > # Create a dictionary for our response data > data = { > 'error': False, > 'message': 'Category %s Added Successfully' % > form.cleaned_data['name'], > # Pass a list of the 'name' attribute from each > Category. > # Django model instances are not serializable > 'categories': [c.name for c in cats], > } > else: > # Form was not valid, get the errors from the form and > # create a dictionary for our error response. > data = { > 'error': True, > 'message': "Please try again! one", > 'name_error': str(form.errors.get('name', '')), > 'slug_error': str(form.errors.get('slug', '')), > 'file_error': str(form.errors.get('file', '')), > } > # encode the data as a json object and return it > return http.HttpResponse(json.dumps(data)) > > ## Old Form Handler Logic ## > if form.is_valid(): > form.save() > return http.HttpResponseRedirect('/category/') > else: > form = CategoryForm() > cats = Category.objects.all().order_by('name') > context = Context({'title': 'Add Category', 'form': form, > 'categories': cats}) > return render_to_response('ajax_form.html', > context,context_instance=RequestContext(request)) > > ___________ > ajax_form.html > ___________ > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"> > <head> > <title>{{ title }}</title> > <style> > #message {width:250px; background-color:#aaa;} > .hide {display: none;} > </style> > <script type="text/javascript" src="http://ajax.googleapis.com/ajax/ > libs/jquery/1.3.2/jquery.js"></script> > </head> > <body> > > <script type="text/javascript"> > // prepare the form when the DOM is ready > $(document).ready(function() { > $("#add_cat").ajaxStart(function() { > // Remove any errors/messages and fade the form. > $(".form_row").removeClass('errors'); > $(".form_row_errors").html(''); > $("#add_cat").fadeTo('slow', 0.33); > $("#add_cat_btn").attr('disabled', 'disabled'); > $("#message").addClass('hide'); > }); > > // Submit the form with ajax. > $("#add_cat").submit(function(){ > $.post( > // Grab the action url from the form. > "#add_cat.getAttribute('action')", > > // Serialize the form data to send. > $("#add_cat").serialize(), > > // Callback function to handle the response from view. > function(resp, testStatus) { > if (resp.error) { > // check for field errors > if (resp.name_error != '') { > $("#name_row").addClass('errors'); > $("#name_errors").html(resp.name_error); > } > if (resp.slug_error != '') { > $("#slug_row").addClass('errors'); > $("#slug_errors").html(resp.slug_error); > } > if (resp.file_error != '') { > $("#file_row").addClass('errors'); > $("#file_errors").html(resp.file_error); > } > } else { > // No errors. Rewrite the category list. > $("#categories").fadeTo('fast', 0); > var text = new String(); > for(i=0; i<resp.categories.length ;i++){ > var m = resp.categories[i] > text += "<li>" + m + "</li>" > } > $("#categories").html(text); > $("#categories").fadeTo('slow', 1); > $("#id_name").attr('value', ''); > $("#id_slug").attr('value', ''); > $("#id_file").attr('value', ''); > } > // Always show the message and re-enable the form. > $("#message").html(resp.message); > $("#message").removeClass('hide'); > $("#add_cat").fadeTo('slow', 1); > $("#add_cat_btn").attr('disabled', ''); > > // Set the Return data type to "json". > }, "json"); > return false; > }); > > }); > </script> > > <h1>{{ title }}</h1> > <div id='message'></div> > <form id="add_cat" method="post" action="." enctype="multipart/ > form-data"> > {% csrf_token %} > > <div class='form_row' id='name_row'> > <p id='name_errors' class="form_row_errors">{% if > form.name.errors %}{{ form.name.errors }}{% endif %}</p> > {{ form.name.label_tag }}{{ form.name }} > </div> > <div class='form_row' id='slug_row'> > <p id='slug_errors' class="form_row_errors">{% if > form.slug.errors %}{{ form.slug.errors }}{% endif %}</p> > {{ form.slug.label_tag }}{{ form.slug }} > </div> > > <div class='form_row' id='file_row'> > <p id='file_errors' class="form_row_errors">{% if > form.file.errors %}{{ form.file.errors }}{% endif %}</p> > {{ form.file.label_tag }}{{ form.file }} > </div> > > <input id="add_cat_btn" type='submit' value="save"> > </form> > <hr> > <h2>Categories</h2> > <ul id="categories"> > {% for cat in categories %} > <li>{{ cat.name }}</li> > {% endfor %} > </ul> > </body> > </html>
-- 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.