django-views

django - form has no errors but form.is_valid() doesn't validate

纵饮孤独 提交于 2019-11-27 12:15:27
问题 I have a form that for some reason it it doesn't throw any errors (if user adds data in the text field) but form.is_valid() doesn't validate. Any ideas? forms.py: class MyForm(forms.Form): title = forms.CharField(widget=forms.TextInput(attrs={'class':'titleField')) mytemplate.html <form action="" method="post" name="form">{% csrf_token %} {{ form.title.errors }} {{ form.title }} <input type="submit" name='submit_button' value="Post" /> </form> views.py: if 'submit_button' in request.POST:

int() argument must be a string or a number, not 'SimpleLazyObject'

北城以北 提交于 2019-11-27 11:44:29
问题 I got, following Error messages, TypeError at /save/ int() argument must be a string or a number, not 'SimpleLazyObject' While executing following form. views.py def bookmark_save_page(request): if request.method == 'POST': form = BookmarkSaveForm(request.POST) if form.is_valid(): # create or get link link, dummy = Link.objects.get_or_create( url = form.cleaned_data['url'] ) # create or get bookmark bookmark, created = Bookmark.objects.get_or_create( user=request.user, link=link ) # update

How to set up custom middleware in Django

浪尽此生 提交于 2019-11-27 11:11:02
I am trying to create middleware to optionally pass a kwarg to every view that meets a condition. The problem is that I cannot find an example of how to set up the middleware. I have seen classes that override the method I want to, process_view : Class CheckConditionMiddleware(object): def process_view(self, request): return None But where do I put this class? Do I create a middleware app and put this class inside of it and then reference it in settings.middleware ? First: The path structure If you don't have it you need to create the middleware folder within your app following the structure:

What is the equivalent of “none” in django templates?

﹥>﹥吖頭↗ 提交于 2019-11-27 11:01:54
问题 I want to see if a field/variable is none within a Django template. What is the correct syntax for that? This is what I currently have: {% if profile.user.first_name is null %} <p> -- </p> {% elif %} {{ profile.user.first_name }} {{ profile.user.last_name }} {% endif%} In the example above, what would I use to replace "null"? 回答1: None, False and True all are available within template tags and filters. None, False , the empty string ( '', "", """""" ) and empty lists/tuples all evaluate to

How do I set user field in form to the currently logged in user?

社会主义新天地 提交于 2019-11-27 10:54:03
问题 I'm making an election information app, and I want to allow the currently logged-in user to be able to declare himself and only himself as a candidate in an election. I'm using Django's built-in ModelForm and CreateView. My problem is that the Run for Office form (in other words, the 'create candidate' form) allows the user to select any user in the database to make a candidate. I want the user field in the Run for Office to be automatically set to the currently logged-in user, and for this

What is the right way to validate if an object exists in a django view without returning 404?

倖福魔咒の 提交于 2019-11-27 10:46:31
I need to verify if an object exists and return the object, then based on that perform actions. What's the right way to do it without returning a 404? try: listing = RealEstateListing.objects.get(slug_url = slug) except: listing = None if listing: I would not use the 404 wrapper if you aren't given a 404. That is misuse of intent. Just catch the DoesNotExist, instead. try: listing = RealEstateListing.objects.get(slug_url=slug) except RealEstateListing.DoesNotExist: listing = None zzart You can also do: if not RealEstateListing.objects.filter(slug_url=slug).exists(): # do stuff... Sometimes it

Deleting objects in Django

点点圈 提交于 2019-11-27 10:21:55
问题 In a mini blog app, I want to create a delete function, so that the owner of the blog can delete his entries (and only his entries). I guess that the only methods for doing do, is using a form. Though my the deletion code seems clear and correct, it doesn't work. My code: def delete_new(request,id): u = New.objects.get(pk=id).delete() if request.method == 'POST': form = DeleteNewForm(request.POST) form.u.delete() form.save() return render_to_response('news/deleteNew.html', { 'form': form, },

Class has no objects member

夙愿已清 提交于 2019-11-27 10:14:23
def index(request): latest_question_list = Question.objects.all().order_by('-pub_date')[:5] template = loader.get_template('polls/index.html') context = {'latest_question_list':latest_question_list} return HttpResponse(template.render(context, request)) The first line of that function gets an error on Question.objects.all() -->E1101: Class 'Question has no objects member` Im following the Django documentation tutorial and they have the same code up and running. I have tried calling an instance. Question = new Question() and using MyModel.objects.all() Also my models.py code for that class is

Delete multiple objects in django

谁说胖子不能爱 提交于 2019-11-27 10:12:33
问题 I need to select several objects to be deleted from my database in django using a webpage. There is no category to select from so I can't delete from all of them like that. Do I have to implement my own delete form and process it in django or does django have a way to already do this? As its implemented in the admin interface. 回答1: You can delete any QuerySet you'd like. For example, to delete all blog posts with some Post model Post.objects.all().delete() and to delete any Post with a future

How to Create a form from a json-schema?

女生的网名这么多〃 提交于 2019-11-27 10:11:35
问题 How to create form from JSON Schema? I am writing code in JavaScript and jquery. With this template part like Form I am creating this with haml and adding this in js file. For backend I am using python. I am using Django framework. So I got some links for create form from JSON Schema. Reference link : http://neyric.github.io/inputex/examples/json-schema.html In my Form : Input elemets : textboxes, textarea, select list, submit and cancel buttons are present. So I want to ask is creating form