Tuesday, October 7, 2014

Re: How do I create a simple user view web page to create/add mysql db entries?



On Tuesday, October 7, 2014 4:49:04 PM UTC-7, Collin Anderson wrote:
> Am I correct in understanding that using ModelForm example you listed as a view does not need to be added to urls then? What is the user access page then?
The add_view would need to be added to your urls.

I added the add_view to teh urls.py

from django.conf.urls import patterns, url

from oneidentry import views

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^$', views.add_view, name='add_view'),

But received an error that the view was not defined. I placed the code above into a new forms.py file:

from django import ModelForm
from oneidentry.models import Hardwareid

class HardwareidForm(forms.ModelForm):
     class Meta:
        model = Hardwareid
        fields = ['hardwareid_text']

def add_view(request):
    form = HardwareidForm()
    if request.method == 'POST':
        form = HardwareidForm(request.POST)
        if form.is_valid():
            the_new_entry = form.save()
            return redirect()  # wherever you want to go next
    return render(request, 'base_site.html', {'form': form})

Is that correct? Or does this belong in views.py?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e6efaffc-7548-46ec-9359-9dfe359ff015%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment