Django: how to create a table and then update the database through them

I'm new to Django, and I tried my best to figure it out, but I still have a long way to go. I am working on a project in which I have to create a web page where I have to display data from these classes:

class team(models.Model): team_name = models.CharField(max_length = 40) def __unicode__(self): return self.team_name class metric(models.Model): team = models.ForeignKey(team) metric_name = models.CharField(max_length = 40) def __unicode__(self): return self.metric_name class members(models.Model): metric = models.ForeignKey(metric) member_ID = models.CharField(max_length = 40) member_name = models.CharField(max_length = 40, null=True, blank=True) def __unicode__(self): return self.member_ID 

In addition, I want this data to be editable as well. So, there should be an “Edit” button, which, after clicking, will allow the user to edit these fields, and as soon as he saves, the database will automatically be updated.

Now, as I understand it. Will I need to display this data using some kind of table, and then, as soon as the user clicks the "Change" button, will I need to change the Tables into a form so that he can edit the data? and then update the database as soon as it clicks on save.

I'm not sure how to do this in Django. If someone could refer to me step by step on how to proceed further and possibly refer to some links. It helped me a lot.

Thanks:)

UPDATE: I forgot to mention this before. I already created a page where the user selects group_name .. Then he is redirected to another page where I want to show the table for this particular command. And I also want the page to be editable. If I use modelforms, then I will not be able to access one object of my command model.

UPDATE2: I still adhere to this. I can’t understand how to display only a few elements and variables of the model class, and not all. In addition, I cannot understand how to create a form that can receive input from a user for whom the database object must edit, and then show that some objects are subject to editing.

+4
source share
2 answers

If you want to implement it as dynamically as you described, then this is not possible for Django; Django can serve you as a table and forms, but dynamically changing without reloading the page (which I guess is what you really need) needs some JavaScript client code.

But about showing data and forms: use generic Django-based class views.

ListView and DetailsView should get you started by displaying data from models.

ModelForm in combination with the corresponding universal types of editing should help you with data editing.

These views will by default return data in some - usually useful format and can be customized using a custom template. I suggest you familiarize yourself with the links that I have provided to you and work from there. That should make you go.

+1
source

In connection with the comments here and here . First of all, you should write your class names in capital letters. Please check the documentation .

You create forms for a set of queries like this.

forms.py

 from django.forms import ModelForm class MembersForm(ModelForm): class Meta: model = Members 

urls.py

 from django.conf.urls.defaults import * urlpatterns = patterns('yourapp.views', url(r'^$', # / view = 'index', name = 'team-index', ), url(r'^(?P<lookup_team>[-\w]+)/$', # .../team-extreme/ view = 'update', name = 'update-members', ), ) 

views.py (of your application)

 from django.http import HttpResponse from django.template.context import RequestContext from django.shortcuts import render_to_response, reverse, redirect from yourapp.models import Members from yourapp.forms import MembersForm def index(request): queryset = Members.objects.all() template = 'yourapp/index.html' kwvars = { 'data': queryset, } return render_to_response(template, kwvars, RequestContext(request)) def update(request, lookup_team): """change members being in a special team""" queryset = Members.objects.filter(metric__team__team_name=lookup_team) if request.POST: form = MemberForm(request.POST, instance=queryset) if form.is_valid(): form.save() return redirect(reverse('team-index')) else: form = MemberForm(instance=queryset) template = 'yourapp/update.html' kwvars = { 'form': form, } return render_to_response(template, kwvars, RequestContext(request)) 

Hope this helps. If you have any questions, I got a comment.

+3
source

Source: https://habr.com/ru/post/1439017/


All Articles