Django Redirect with Python Variables - python

So I'm currently working on a Django project where an object with an id is created from a form, and that id is needed in the form that the first form redirects to. Is there a way I can pass this id (in python) using redirect? I can't seem to figure out how to do this, but I'm sure there is a way since it seems like it would be pretty useful.
Thank you all,
Alex

Related

Creating reusable forms/views in Flask

I am not sure what would be the best route to go down, or I may be missing something obvious.
Example I can give is I have 'person' model and associated form, and view created to add a new 'person'. This all works great. What I would like to do though is use this 'view/form' in a master page with other similar 'views/forms'. With each part being able to add/edit or delete a record from each sub view/form.
So I have all functionality done, just don't know how I can create this master page with child objects, but these child objects can be their own page as well, type of thing.
The idea being that the master page structure is flexible and can accommodate various elements based on the context the user is in.
Should I be looking at blueprints or Jinja2 and its template structure. Or is it how I am handling routes within the main app.
Apologies if this is too vague.
I have done this using AngularJS ng-include directive. You can include whatever html you want, but be careful with Jinja2. If the html you are trying to include contains any script tag it will crash. See my question here. If you need to import a form that needs a script tag you will need to make sure it is not loaded when you are pushing it with Angular. Since it makes a xhr request, you can use flask.request.is_xhr to check if it is angular or the user that is requiring the form. You cannot forget to add this to your angular app
Otherwise is_xhr will always return false.
myAppModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
Be careful with base_ templates as well, since they usually load script tags. You can pass the base template through your flask route, and make it extend a blank base html when the request is made through angular. I wish I had my example here, but I am on the bus. Let me know how far you got.

How a django form is passed to a html template

I am new in Django framework and trying to understand how it works and what is its structure. I am just curious about how a django form is passed to a html template ? Any help would be appriciated.
I think you are curious in how django converts a python object into HTML, its internal mechanism, so the tutorial might not cover what it is.
I did have the same question before, but if you do look at code in forms.py https://github.com/django/django/blob/master/django/forms/forms.py you will see that internally there are methods which will look at the attributes of the objects that you have declared and generate snipplets of html code which will then be rendered.
Of course I cant tell you exactly how it works, I leave the heavy lifting to django.. Isn't that why we use a modern web framework like django in the first place.
Hope you will find this useful

Validating checkboxes from form in Django

Relatively new to Python..
I would like to know how to check if a check box has been selected.
I am reading the form values into a python script and am unsure how to go about validating.
Using the Web Framework Django. The form is being posted back to a views.py script.
I am loading up a dictionary with many different variables. These variables need to then be passed to a shell script, the command to the shell script requires these check box values to be returned in the form of 0 or 1 in order to create a zip file.
I understand this code is not python but it is my understanding of what i am trying to achieve:
if variable.checked == 'yes'
return 1
elif
return 0
The documentation for Django is really quite good. Check out the description of the HttpRequest class for more information on how this part works. The short answer is that you can do something like this:
def myview(request):
# do stuff
option_checked = 'myvariable.checkboxoption' in request.REQUEST
# do more stuff and return the response
That being said, Django also has a comprehensive form widget/validation/extraction subsystem. I'd recommend going through the full tutorial (particularly part 4, which covers forms) and checking out the mailing list to answer any more specific questions about how to use these libraries.

Forms generated through admin in Django

I need to be able to create forms from admin panel. Process would look like this:
I click on "Add form" then I enter email to which the form should be sent and of course several fields (probably thanks to inlines) consisting of field name, type and if it is required. User should be able to view and fill the form and submit it and the data should be sent to the email given in admin.
Everything looks pretty straightforward but from my point of view it need some metaclass programming skills.
Could anyone point me to a goot form builder for Django or at least hand some tips about creating such thing? I found django-forms-builder but it is a bit too restricted imho.
I know this one's a few months old but I just though I'd post an update here anyway for anyone else that comes along.
django-forms-builder has just been rewritten to do exactly what you were looking for when you originally posted this question.
You can find the new version at http://github.com/stephenmcd/django-forms-builder or http://bitbucket.org/stephenmcd/django-forms-builder
There are many alternatives, although not many of them are actively maintained:
https://www.djangopackages.com/grids/g/form-builder/
If you want to have a full control of what's happening (change fields for your needs or add new ones, add captcha or honeypot, add custom handling of form data, use form wizards or even use your forms via web REST API), use django-fobi https://pypi.python.org/pypi/django-fobi

Django-registration and ReCaptcha integration - how to pass the user's IP

New to django and trying to setup django-registration 0.8 with recaptcha-client. I followed the advice posted in the answer to this question.
I used the custom form and custom backend from that post and the widget and field from this tutorial. My form is displaying properly with the recaptcha widget but when I submit it throws the error about the missing IP. What's the best way to pass the IP using django-registration?
I also used the code from the tutorial you linked, in my case to add reCaptcha to the django comments app.
You need something like initial={'captcha': request.META['REMOTE_ADDR']} at the point where your RecaptchaRegistrationForm gets instantiated.
Unfortunately this is buried in the registration/views.py register method.
You need to do something like copy and paste their code into a view method of your own and edit it. Then you need a urls.py for your customised backend that looks like the one in registration/backends/default/ but points to your new register view in place of theirs.

Categories