How to structure views for database pulling and manipulationHow to combine 2 or more querysets in a Django view?Saving form data rewrites the same rowSharing data between two views through template in Django?Django: can view return render to ajax calls?building a dashboard with generic views in djangoPassing multiple values from user input to Django viewHow to edit & save contents from front-end in Django?Django: Returning a customized view functionTemplate variables based on calculations using fields from Django ModelDjango redirect to a view with GET arguments set
Character is called by their first initial. How do I write it?
Why are so many countries still in the Commonwealth?
Marrying a second woman behind your wife's back: is it wrong and can Quran/Hadith prove this?
How do I address my Catering staff subordinate seen eating from a chafing dish before the customers?
Print sums of all subsets
Are there any examples of technologies have been lost over time?
How to deal with a player who makes bad characters and kills them?
What do teaching faculty do during semester breaks?
Commercial jet accompanied by small plane near Seattle
"I you already know": is this proper English?
Integral of the integral using NIntegrate
Timing/Stack question about abilities triggered during combat
How important is a good quality camera for good photography?
How can I stop myself from micromanaging other PCs' actions?
What's the difference between 2a and 10a charging options?
Examples of simultaneous independent breakthroughs
Problem in styling a monochrome plot
What is a Waiting Word™?
Is there a reason why I should not use the HaveIBeenPwned API to warn users about exposed passwords?
High income, sudden windfall
Iterate over non-const variables in C++
Is it legal for private citizens to "impound" e-scooters?
TSA asking to see cell phone
Area of parallelogram = Area of square. Shear transform
How to structure views for database pulling and manipulation
How to combine 2 or more querysets in a Django view?Saving form data rewrites the same rowSharing data between two views through template in Django?Django: can view return render to ajax calls?building a dashboard with generic views in djangoPassing multiple values from user input to Django viewHow to edit & save contents from front-end in Django?Django: Returning a customized view functionTemplate variables based on calculations using fields from Django ModelDjango redirect to a view with GET arguments set
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am having difficulties understanding how best to structure my views.
I am pulling data on various users and creating variables that summarise some of these variables (such as occurrences per week etc). This is so I can graph these summary variables in my templates. I am doing quite a lot of different manipulations which is getting quite messy , and i shall need these manipulations for other templates. Can somebody recommend how best to structure views in this case. I think using classes is the solution to use the same functions for other templates but I cannot quite understand how. I also feel there must be a better way to structure each manipulation of database data.
def dashboard(request):
posts= Post.objects.filter(user=request.user)
posts_count = posts.count()
post_early = Post.objects.filter(user=request.user).earliest('date') #need to extract the date value from this so I can take the difference
total_days = (datetime.datetime.now().date()- post_early.date).days
average_30days= round((posts_count/total_days)*30,2)
list4=[]
list5=[]
i=1
time3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list4.append(days2(time3,request,Post))
list5.append(time3.strftime('%b %d, %Y'))
i+=1
time3=time3 + datetime.timedelta(+1)
django django-views django-database
add a comment |
I am having difficulties understanding how best to structure my views.
I am pulling data on various users and creating variables that summarise some of these variables (such as occurrences per week etc). This is so I can graph these summary variables in my templates. I am doing quite a lot of different manipulations which is getting quite messy , and i shall need these manipulations for other templates. Can somebody recommend how best to structure views in this case. I think using classes is the solution to use the same functions for other templates but I cannot quite understand how. I also feel there must be a better way to structure each manipulation of database data.
def dashboard(request):
posts= Post.objects.filter(user=request.user)
posts_count = posts.count()
post_early = Post.objects.filter(user=request.user).earliest('date') #need to extract the date value from this so I can take the difference
total_days = (datetime.datetime.now().date()- post_early.date).days
average_30days= round((posts_count/total_days)*30,2)
list4=[]
list5=[]
i=1
time3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list4.append(days2(time3,request,Post))
list5.append(time3.strftime('%b %d, %Y'))
i+=1
time3=time3 + datetime.timedelta(+1)
django django-views django-database
add a comment |
I am having difficulties understanding how best to structure my views.
I am pulling data on various users and creating variables that summarise some of these variables (such as occurrences per week etc). This is so I can graph these summary variables in my templates. I am doing quite a lot of different manipulations which is getting quite messy , and i shall need these manipulations for other templates. Can somebody recommend how best to structure views in this case. I think using classes is the solution to use the same functions for other templates but I cannot quite understand how. I also feel there must be a better way to structure each manipulation of database data.
def dashboard(request):
posts= Post.objects.filter(user=request.user)
posts_count = posts.count()
post_early = Post.objects.filter(user=request.user).earliest('date') #need to extract the date value from this so I can take the difference
total_days = (datetime.datetime.now().date()- post_early.date).days
average_30days= round((posts_count/total_days)*30,2)
list4=[]
list5=[]
i=1
time3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list4.append(days2(time3,request,Post))
list5.append(time3.strftime('%b %d, %Y'))
i+=1
time3=time3 + datetime.timedelta(+1)
django django-views django-database
I am having difficulties understanding how best to structure my views.
I am pulling data on various users and creating variables that summarise some of these variables (such as occurrences per week etc). This is so I can graph these summary variables in my templates. I am doing quite a lot of different manipulations which is getting quite messy , and i shall need these manipulations for other templates. Can somebody recommend how best to structure views in this case. I think using classes is the solution to use the same functions for other templates but I cannot quite understand how. I also feel there must be a better way to structure each manipulation of database data.
def dashboard(request):
posts= Post.objects.filter(user=request.user)
posts_count = posts.count()
post_early = Post.objects.filter(user=request.user).earliest('date') #need to extract the date value from this so I can take the difference
total_days = (datetime.datetime.now().date()- post_early.date).days
average_30days= round((posts_count/total_days)*30,2)
list4=[]
list5=[]
i=1
time3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list4.append(days2(time3,request,Post))
list5.append(time3.strftime('%b %d, %Y'))
i+=1
time3=time3 + datetime.timedelta(+1)
django django-views django-database
django django-views django-database
edited Mar 26 at 17:37
Nafees Anwar
2,2841 gold badge7 silver badges22 bronze badges
2,2841 gold badge7 silver badges22 bronze badges
asked Mar 26 at 17:32
Matthew MacfarlaneMatthew Macfarlane
175 bronze badges
175 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
def dashboardView(request):
posts = Post.objects.filter(user=request.user)
posts_count = posts.count()
#need to extract the date value from post_early so I can take the difference
post_early = Post.objects.filter(user=request.user).earliest('date')
total_days = (datetime.datetime.now().date() - post_early.date).days
average_30days = round((posts_count/total_days)*30,2)
list_4 = []
list_5 = []
i = 1
time_3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list_4.append(days2(time_3, request, Post))
list_5.append(time_3.strftime('%b %d, %Y'))
i += 1
time_3 = time_3 + datetime.timedelta(1)
I'd do something like this. There were a few inconsistency:
-keep a space before and a space after operators (=, *, -, +, ...).
-I'd consider a good practice to always suffix -View to your views, but it's just personal preference
-Use empty lines to separate blocks of code, not groups of variables. If you have a long list of variables declarations (not this case) you can use comments to separate and categorize them.
-Use list_3 instead of list3 (and similar cases), it's more readable.
For more you can always check the official python style guide: https://www.python.org/dev/peps/pep-0008/
Anyway, if you're consistent and attain to the coding style used in the Django documentation while learning, you'll be fine.
##### EDIT:
Note: my answer is based on the code you provided, which seems cut (no return statement?) and without the other modules.
You're using a function based view which is not wrong nor correct, just one of the possible choices. If you don't like it, or want to try something else, a ListView may work for you: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/
Example:
from django.views import ListView
class DashboardView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Post.objects.filter(user=request.user)
# add all the data you need to the context dictionary
return context
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363089%2fhow-to-structure-views-for-database-pulling-and-manipulation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
def dashboardView(request):
posts = Post.objects.filter(user=request.user)
posts_count = posts.count()
#need to extract the date value from post_early so I can take the difference
post_early = Post.objects.filter(user=request.user).earliest('date')
total_days = (datetime.datetime.now().date() - post_early.date).days
average_30days = round((posts_count/total_days)*30,2)
list_4 = []
list_5 = []
i = 1
time_3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list_4.append(days2(time_3, request, Post))
list_5.append(time_3.strftime('%b %d, %Y'))
i += 1
time_3 = time_3 + datetime.timedelta(1)
I'd do something like this. There were a few inconsistency:
-keep a space before and a space after operators (=, *, -, +, ...).
-I'd consider a good practice to always suffix -View to your views, but it's just personal preference
-Use empty lines to separate blocks of code, not groups of variables. If you have a long list of variables declarations (not this case) you can use comments to separate and categorize them.
-Use list_3 instead of list3 (and similar cases), it's more readable.
For more you can always check the official python style guide: https://www.python.org/dev/peps/pep-0008/
Anyway, if you're consistent and attain to the coding style used in the Django documentation while learning, you'll be fine.
##### EDIT:
Note: my answer is based on the code you provided, which seems cut (no return statement?) and without the other modules.
You're using a function based view which is not wrong nor correct, just one of the possible choices. If you don't like it, or want to try something else, a ListView may work for you: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/
Example:
from django.views import ListView
class DashboardView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Post.objects.filter(user=request.user)
# add all the data you need to the context dictionary
return context
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
add a comment |
def dashboardView(request):
posts = Post.objects.filter(user=request.user)
posts_count = posts.count()
#need to extract the date value from post_early so I can take the difference
post_early = Post.objects.filter(user=request.user).earliest('date')
total_days = (datetime.datetime.now().date() - post_early.date).days
average_30days = round((posts_count/total_days)*30,2)
list_4 = []
list_5 = []
i = 1
time_3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list_4.append(days2(time_3, request, Post))
list_5.append(time_3.strftime('%b %d, %Y'))
i += 1
time_3 = time_3 + datetime.timedelta(1)
I'd do something like this. There were a few inconsistency:
-keep a space before and a space after operators (=, *, -, +, ...).
-I'd consider a good practice to always suffix -View to your views, but it's just personal preference
-Use empty lines to separate blocks of code, not groups of variables. If you have a long list of variables declarations (not this case) you can use comments to separate and categorize them.
-Use list_3 instead of list3 (and similar cases), it's more readable.
For more you can always check the official python style guide: https://www.python.org/dev/peps/pep-0008/
Anyway, if you're consistent and attain to the coding style used in the Django documentation while learning, you'll be fine.
##### EDIT:
Note: my answer is based on the code you provided, which seems cut (no return statement?) and without the other modules.
You're using a function based view which is not wrong nor correct, just one of the possible choices. If you don't like it, or want to try something else, a ListView may work for you: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/
Example:
from django.views import ListView
class DashboardView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Post.objects.filter(user=request.user)
# add all the data you need to the context dictionary
return context
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
add a comment |
def dashboardView(request):
posts = Post.objects.filter(user=request.user)
posts_count = posts.count()
#need to extract the date value from post_early so I can take the difference
post_early = Post.objects.filter(user=request.user).earliest('date')
total_days = (datetime.datetime.now().date() - post_early.date).days
average_30days = round((posts_count/total_days)*30,2)
list_4 = []
list_5 = []
i = 1
time_3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list_4.append(days2(time_3, request, Post))
list_5.append(time_3.strftime('%b %d, %Y'))
i += 1
time_3 = time_3 + datetime.timedelta(1)
I'd do something like this. There were a few inconsistency:
-keep a space before and a space after operators (=, *, -, +, ...).
-I'd consider a good practice to always suffix -View to your views, but it's just personal preference
-Use empty lines to separate blocks of code, not groups of variables. If you have a long list of variables declarations (not this case) you can use comments to separate and categorize them.
-Use list_3 instead of list3 (and similar cases), it's more readable.
For more you can always check the official python style guide: https://www.python.org/dev/peps/pep-0008/
Anyway, if you're consistent and attain to the coding style used in the Django documentation while learning, you'll be fine.
##### EDIT:
Note: my answer is based on the code you provided, which seems cut (no return statement?) and without the other modules.
You're using a function based view which is not wrong nor correct, just one of the possible choices. If you don't like it, or want to try something else, a ListView may work for you: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/
Example:
from django.views import ListView
class DashboardView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Post.objects.filter(user=request.user)
# add all the data you need to the context dictionary
return context
def dashboardView(request):
posts = Post.objects.filter(user=request.user)
posts_count = posts.count()
#need to extract the date value from post_early so I can take the difference
post_early = Post.objects.filter(user=request.user).earliest('date')
total_days = (datetime.datetime.now().date() - post_early.date).days
average_30days = round((posts_count/total_days)*30,2)
list_4 = []
list_5 = []
i = 1
time_3=datetime.datetime.now() + datetime.timedelta(-30)
while i<32:
list_4.append(days2(time_3, request, Post))
list_5.append(time_3.strftime('%b %d, %Y'))
i += 1
time_3 = time_3 + datetime.timedelta(1)
I'd do something like this. There were a few inconsistency:
-keep a space before and a space after operators (=, *, -, +, ...).
-I'd consider a good practice to always suffix -View to your views, but it's just personal preference
-Use empty lines to separate blocks of code, not groups of variables. If you have a long list of variables declarations (not this case) you can use comments to separate and categorize them.
-Use list_3 instead of list3 (and similar cases), it's more readable.
For more you can always check the official python style guide: https://www.python.org/dev/peps/pep-0008/
Anyway, if you're consistent and attain to the coding style used in the Django documentation while learning, you'll be fine.
##### EDIT:
Note: my answer is based on the code you provided, which seems cut (no return statement?) and without the other modules.
You're using a function based view which is not wrong nor correct, just one of the possible choices. If you don't like it, or want to try something else, a ListView may work for you: https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-display/
Example:
from django.views import ListView
class DashboardView(ListView):
model = Post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['posts'] = Post.objects.filter(user=request.user)
# add all the data you need to the context dictionary
return context
edited Mar 26 at 20:10
answered Mar 26 at 18:34
ShynrasShynras
216 bronze badges
216 bronze badges
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
add a comment |
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
this doesn't answer my question at all. I am asking about structure using classes other any other options not line spacing and naming conventions.
– Matthew Macfarlane
Mar 26 at 19:17
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
Sorry then, I thought you were asking how to organize your code (which definitely wouldn't hurt in this case, and would help preventing from getting messy. I'll try to answer your question with an edit to my previous answer.
– Shynras
Mar 26 at 19:51
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363089%2fhow-to-structure-views-for-database-pulling-and-manipulation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown